0

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?

4

3 回答 3

0

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)
于 2012-05-17T20:19:48.107 回答
0
function cmp($a, $b)
{
    if ($a ['order'] == $b ['order']) {
        return 0;
    }
    return ($a ['order'] < $b ['order']) ? -1 : 1;
}

usort($array, "cmp");

print_r ($array);
于 2012-05-17T20:02:48.277 回答
0

一个相当简单的解决方案是使用数组索引本身来存储顺序,然后使用sort对数组进行排序,然后array_values重新索引它?

于 2012-05-17T20:07:11.373 回答