排序一个很大的多维数组 [英] Sorting a big multidimensional array
本文介绍了排序一个很大的多维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个包含以下数据一个非常大的数组:
I have a really big array containing the following data:
Array
(
[2] => Array
(
[Berlin] => Array
(
[1] => Array
(
[0] => stdClass Object
(
[name] => Joe
[car_name] => Audi
[car_color] => Black
[sid] => 130
)
[1] => stdClass Object
(
[name] => Mark
[car_name] => BMW
[car_color] => Red
[sid] => 135
)
)
[0] => Array
(
[0] => stdClass Object
(
[name] => Lucas
[car_name] => Audi
[car_color] => Yellow
[sid] => 168
)
[1] => stdClass Object
(
[name] => Joe
[car_name] => Volkswagen
[car_color] => Black
[sid] => 170
)
[2] => stdClass Object
(
[name] => Thomas
[car_name] => Ford
[car_color] => Gray
[sid] => 119
)
)
)
[Moscow] => Array
(
[1] => Array
(
[0] => stdClass Object
(
[name] => Matt
[car_name] => Mustang
[car_color] => Black
[sid] => 230
)
[1] => stdClass Object
(
[name] => Suze
[car_name] => Lada
[car_color] => Red
[sid] => 245
)
)
[0] => Array
(
[0] => stdClass Object
(
[name] => Lucas
[car_name] => Unknown
[car_color] => Brown
[sid] => 374
)
[1] => stdClass Object
(
[name] => Mathew
[car_name] => Volkswagen
[car_color] => Blue
[sid] => 589
)
[2] => stdClass Object
(
[name] => Thomas
[car_name] => Ford
[car_color] => Light Blue
[sid] => 741
)
)
)
)
)
现在,我想这样的顺序来排序(请阅读 //
行):
Now I would like to sort it like this (please read the //
lines):
Array
(
[2] => Array //from min to max - eg. from 0 to 50
(
[Berlin] => Array //alphabetically from A to Z
(
[1] => Array //from min to max eg. from 0 to 1
(
)
)
)
)
如何在PHP中实现这一目标?我希望我是很清晰,随时在评论索要额外的信息。
How can I achieve this in PHP? I hope I was clear enough, feel free to ask in the comments for the additional info.
推荐答案
将是这样的:
// ksort sorts the keys low to high
ksort($array[2]);
// uksort w/ natsort is a full alphanumeric sorting A-Z0-9
uksort($array[2]['Berlin'], 'natsort');
ksort($array[2]['Berlin'][1]);
在阵列和阵列功能文档:
Documentation on Array and Array Functions:
http://us3.php.net/manual/en/book。 array.php
功能实例
这例子是真实的特定于您的例子;但是,你可以做一个递归形式的函数,如果你的阵列结构确实是动态的。
This example is real specific to your example; however, you could make a recursive style function if your array structure is really dynamic.
$BigArray = array();
//Sorting the initial array index
ksort($BigArray);
foreach($BigArray as $a)
{
// Sorting Berlin / Moscow here..
uksort($a, 'natsort');
foreach($a as $b)
{
// Sorting the index inside the cities
ksort($b);
}
}
这篇关于排序一个很大的多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文