在 PHP 中按子数组的值对数组进行排序 [英] Sort an array by a child array's value in PHP
本文介绍了在 PHP 中按子数组的值对数组进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个由数组组成的数组.我想通过子数组的属性对父数组进行排序.举个例子
I have an array composed of arrays. I want to sort the parent array by a property of the child arrays. Here's an example
array(2){
[0]=> array(3){
[0]=> string(6) "105945"
[1]=> string(10) "First name"
[2]=> float(0.080878465391)
}
[1]=> array(3) {
[0]=> string(6) "109145"
[1]=> string(11) "Second name"
[2]=> float(0.0504154818384)
}
}
我想通过 [2]
在子数组中升序对父数组进行排序,因此在这种情况下,结果将是子数组颠倒 (.05
, 08
).这是否可以使用众多 PHP 排序函数中的任何一个?
I would like to sort the parent array by [2]
ascending in the child arrays, so in this case the result would be the child arrays reversed (.05
, 08
). Is this possible using any of the numerous PHP sort functions?
推荐答案
您可以使用 usor 函数为:
You can make use of usort function as:
$arr = array(
array("105945","First name",0.080878465391),
array("109145","Second name",0.0504154818384)
);
function cmp($a, $b) {
if ($a[2] == $b[2]) {
return 0;
}
return ($a[2] < $b[2]) ? -1 : 1;
}
usort($arr,"cmp");
这篇关于在 PHP 中按子数组的值对数组进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文