排序PHP中的孩子数组值的数组 [英] Sort an array by a child array's value in PHP

查看:122
本文介绍了排序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?

推荐答案

您可以使用 usort 功能:

$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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆