I'm searching for help. I found ksort and other sort methods for php. But i got an Array in an Array with an key called order. And this i want to be the order of the array

array(
   array("obj" => $objPage, "id" => "LogIn", "order" => 3),
   array("obj" => $objPage, "id" => "Home", "order" => 1),
   array("obj" => $objPage, "id" => "Register", "order" => 2),
   array("obj" => $objPage, "id" => "Imprint", "order" => 4), /* ... */
)

Now i want the array objects to sort as this:

array(
   array("obj" => $objPage, "id" => "Home", "order" => 1),
   array("obj" => $objPage, "id" => "Register", "order" => 2),
   array("obj" => $objPage, "id" => "LogIn", "order" => 3),
   array("obj" => $objPage, "id" => "Imprint", "order" => 4), /* ... */
)

Can you give me a hint how to solve this?

link|improve this question
feedback

3 Answers

function cmp($a, $b)
{
    if ($a ['order'] == $b ['order']) {
        return 0;
    }
    return ($a ['order'] < $b ['order']) ? -1 : 1;
}

usort($array, "cmp");

print_r ($array);
link|improve this answer
feedback

A fairly simple solution would be to use the array index itself to store the order, then use sort to sort the array into order and then array_values to re-index it?

link|improve this answer
feedback

array_multisort may be valid for you:

Suposing your array is called $myArray

foreach ($myArray as $key => $row) {   
    $order[$key] = $row['order'];
}

array_multisort($order, SORT_ASC, $myArray)
link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.