开发者

PHP Sort Array based on values in another [duplicate]

This question already has answers here: Closed 12 years ago.

Possible Duplicate:

PHP - sort an array based on another array?

Need some help regarding array sorting....

I have two arrays. The main one (where the key is the user id) :

$user[31] = 'Tom'

$user[43] = 'Jane'

and another array with the order they should be displayed (where key is the order and value is the user id) 开发者_C百科:

$order[1] = 43

$order[2] = 31

How can I apply the ordering to the main array using the ordering one?

Thanks guys!


Use the keys in $order to select the users from $user in the right order:

$orderedUsers = array();
foreach ($order as $key) {
    $orderedUsers[] = $user[$key];
}


Use this one, it is usefull for your problem

$array = array("size" => "XL", "color" => "gold");
print_r(array_values($array));

Result as :

Array
(
    [0] => XL
    [1] => gold
) 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜