比较只有1数组的数组值 [英] Comparing array values of just 1 array
本文介绍了比较只有1数组的数组值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
下面是我的数组:
Array
(
[0] => Array
(
[0] => Array
(
[0] => West
[1] => 4
)
[1] => Array
(
[0] => South west
[1] => 20
)
)
)
我想实现的是能够比较 $阵列[0] [0] [1]
和 $阵列[0] [ 1] [1]
。故取其较小至上,我得到的价值。因此,在这种情况下,我知道4较小,所以我得到西首,然后向西南方旁边。它应该是动态的,因为我们可以得到2个以上。
What I want to achieve is be able to compare $array[0][0][1]
and $array[0][1][1]
. So whichever is lesser comes first and I get the value. So in this case I know that 4 is lesser so I get West first then South west is next. It should be dynamic since we can get more than 2.
推荐答案
这一个简单的例子,你可以使用:
This a simple example you can use :
$arr = array(
array("name"=>"Bob","age"=>8,"colour"=>"red"),
array("name"=>"Greg","age"=>12,"colour"=>"blue"),
array("name"=>"Andy","age"=>5,"colour"=>"purple"));
$sortArray = array();
foreach($arr as $val){
foreach($val as $key=>$value){
if(!isset($sortArray[$key])){
$sortArray[$key] = array();
}
$sortArray[$key][] = $value;
}
}
$orderby = "age"; //change this to whatever key you want from the array
array_multisort($sortArray[$orderby],SORT_ASC,$arr);
print_r($arr);
这篇关于比较只有1数组的数组值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文