bey23
bey23

Reputation: 348

PHP - Order array associative by specific value

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??

Upvotes: 0

Views: 73

Answers (3)

Jakumi
Jakumi

Reputation: 8374

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

Upvotes: 1

Lucas Martins
Lucas Martins

Reputation: 528

Yeah I was thinking in a similar solution:

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

Upvotes: 0

Furkan E Apaydın
Furkan E Apaydın

Reputation: 324

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
        )
)

Upvotes: 0

Related Questions