在键上对PHP不同格式的多维数组进行排序
问题描述:
我有一个像这样的数组
Array
(
[name] => Array
(
[0] => img/test240.jpg
[1] => img/cs1.jpg
[2] => img/cs2.jpg
[3] => img/cs3.jpg
)
[link] => Array
(
[0] => http://google.com
[1] => http://google.com
[2] => http://facebook.com
[3] => http://orkut.com
)
[order] => Array
(
[0] => 4
[1] => 1
[2] => 2
[3] => 3
)
)
我需要按多维数组中WHICH IS KEY的顺序对其进行排序.这是输出.
I need to sort it by order WHICH IS KEY in Multidimensional array. Here is output.
Array
(
[name] => Array
(
[1] => img/cs1.jpg
[2] => img/cs2.jpg
[3] => img/cs3.jpg
[0] => img/test240.jpg
)
[link] => Array
(
[1] => http://google.com
[2] => http://facebook.com
[3] => http://orkut.com
[0] => http://google.com
)
[order] => Array
(
[1] => 1
[2] => 2
[3] => 3
[0] => 4
)
)
在此您可以看到对订单进行排序的名称以及对链接也根据订单进行排序的时间.我该怎么用php.
In this you can see when order is sorted name and link is also sorted according to the order. How can i do this with php.
答
经过一些研究,我发现了一个像这样的简单解决方案
Well after some research i found a simple solution like this
asort($data['order']);
$keys = array_keys($data['order']);
$data['name'] = array_replace(array_flip($keys), $data['name']);
$data['link'] = array_replace(array_flip($keys), $data['link']);
$data['order'] = array_replace(array_flip($keys), $data['order']);
尽管我不想在所有键上应用array_replace
和array_flip
,但这暂时是完成的.我一定会尝试找到一个指令就能实现的方法.
Although i dont want to apply array_replace
and array_flip
on all the keys but this is done for the time being. I will surely trying to find how i can do it with single instruction.