在PHP数组排序问题 [英] Sorting array problem in PHP
本文介绍了在PHP数组排序问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下阵列
[0] =>排列
(
[ID] => 229
[VAL] => 2
) [3] =>排列
(
[ID] => 237
[VAL] => 1
) [4] =>排列
(
[ID] => 238
[VAL] => 6
)
我需要根据数组中的VAL值这个数组排序,不知道如何做到这一点?
解决方案
函数CMP($ A,$ B)
{
如果($ A [VAL] == $ B [VAL]){
返回0;
}
回报($ A [VAL< $ B [VAL])? -1:1;
}usort($ yourarray,CMP);
这了解详情。
I have the following array
[0] => Array
(
[id] => 229
[val] => 2
)
[3] => Array
(
[id] => 237
[val] => 1
)
[4] => Array
(
[id] => 238
[val] => 6
)
I need to sort this array according to the val values in the array, and do not know how to accomplish this?
解决方案
function cmp($a, $b)
{
if ($a["val"] == $b["val"]) {
return 0;
}
return ($a["val"] < $b["val"]) ? -1 : 1;
}
usort($yourarray, "cmp");
Read this for more information.
这篇关于在PHP数组排序问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文