0

I'm looking for a way to order an array associative by a specific value, I'm not sure if it's possible. I tried it with array_multisort() and usort() functions, but I'm afraid that I can not get it.

Example:

$array[] = array('id' => 74215, 'type' => 'BOX');
$array[] = array('id' => 76123, 'type' => 'UNT');
$array[] = array('id' => 71231, 'type' => '');
$array[] = array('id' => 79765, 'type' => 'UNT');
$array[] = array('id' => 77421, 'type' => 'BOX');

If I want to order by 'BOX', then the array will be:

Array (
    [0] => Array
       (
                    [id] => 77421
                    [type] => 'BOX'
       )
    [1] => Array
       (
                    [id] => 74215
                    [type] => 'BOX'
       )
    [2] => Array
       (
                    [id] => 76123
                    [type] => 'UNT'
       )
    .
    .
    .

I could pass other string like 'UNT', and order by like that. Is this possible??

bey23
  • 313
  • 4
  • 16
  • show what you have tried. – kamal pal Jul 14 '16 at 16:11
  • _"If I want to order by 'BOX'"_ that's not very logical. Do you mean that you want to order by `type`? – FirstOne Jul 14 '16 at 16:19
  • Some references: [How can I sort arrays and data in PHP?](http://stackoverflow.com/questions/17364127/how-can-i-sort-arrays-and-data-in-php) **&&** [PHP Sort Array By SubArray Value](http://stackoverflow.com/questions/2477496/php-sort-array-by-subarray-value) – FirstOne Jul 14 '16 at 16:23
  • Sorry @kamalpal I'm searching in my code but I remove it :( – bey23 Jul 14 '16 at 16:26
  • And no, @FirstOne I don't want to order by type, like I said, I want to order by the value inside, passing a string like 'BOX', and order all items, putting the BOX values first. Thats why I asked if that is possible. Sorry if I don't explain well, my English is rusty. – bey23 Jul 14 '16 at 16:26

3 Answers3

1

I assume you want to "sort" by string match, first all those who match that string, after that all those that don't. Unless you have an archaic php version, this could work:

$sortvalue = 'BOX';
usort($array, function($a, $b) use ($sortvalue) {
         if($a['type'] == $sortvalue) return -1;
         elseif($b['type'] == $sortvalue) return 1;
         else return 0;
    });

this should put any 'BOX' entry to the front of your array.

If all others shall be grouped, instead of return 0 do return $a['type'] < $b['type'].

edit: integrated kamal pal's suggestion/correction

Jakumi
  • 8,043
  • 2
  • 15
  • 32
  • 1
    `$sortvalue` is not accessible inside anonymous function, you need to use closure .. `function ($a, $b) use($sortvalue) ...` .. also see http://stackoverflow.com/questions/18616214/confusion-with-php-closures – kamal pal Jul 14 '16 at 16:35
0

I'm not sure if PHP has it's own function that does that, but i write my own, hope it helps:

function sortArray($array_x, $key)
{
    $added_indexes;
    $new_array;

    for($i=0;$i<count($array_x);$i++)
    {
        if($array_x[$i]['type']==$key){
            $new_array[]=$array_x[$i];
            $added_indexes[]=$i;
        }
    }

    for($i=0;$i<count($array_x);$i++)
    {
        if(!in_array($i,$added_indexes))
        {
            $new_array[]=$array_x[$i];
        }
    }

    return $new_array;
}

So when you do this:

$array[] = array('id' => 74215, 'type' => 'BOX');
$array[] = array('id' => 76123, 'type' => 'UNT');
$array[] = array('id' => 71231, 'type' => '');
$array[] = array('id' => 79765, 'type' => 'UNT');
$array[] = array('id' => 77421, 'type' => 'BOX');

print_r(sortArray($array,"BOX"));

Gives this:

Array
(
    [0] => Array
        (
            [id] => 74215
            [type] => BOX
        )

    [1] => Array
        (
            [id] => 77421
            [type] => BOX
        )

    [2] => Array
        (
            [id] => 76123
            [type] => UNT
        )

    [3] => Array
        (
            [id] => 71231
            [type] => 
        )

    [4] => Array
        (
            [id] => 79765
            [type] => UNT
        )
)
FirstOne
  • 6,033
  • 7
  • 26
  • 45
F.E.A
  • 307
  • 1
  • 12
0

Yeah I was thinking in a similar solution:

usort($array, function ($a, $b) {   
    return $a['type'] == 'BOX' ? -1 : ($b['type'] == 'BOX' ? 1 : 0);
});
Lucas Martins
  • 508
  • 6
  • 9