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

5
  • show what you have tried. Commented Jul 14, 2016 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? Commented Jul 14, 2016 at 16:19
  • Some references: How can I sort arrays and data in PHP? && PHP Sort Array By SubArray Value Commented Jul 14, 2016 at 16:23
  • Sorry @kamalpal I'm searching in my code but I remove it :( Commented Jul 14, 2016 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. Commented Jul 14, 2016 at 16:26

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

1 Comment

$sortvalue is not accessible inside anonymous function, you need to use closure .. function ($a, $b) use($sortvalue) ... .. also see stackoverflow.com/questions/18616214/…
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
        )
)

Comments

0

Yeah I was thinking in a similar solution:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.