【问题标题】:Sort multidimensional array by values按值对多维数组进行排序
【发布时间】:2013-07-03 08:24:44
【问题描述】:

我有一个多维数组,我正在尝试按每个数组中的值对其进行排序。

我并没有真正按照其中的值对多维数组进行排序,我自己也没有快乐。

我给出了下面的数组以及按数组中的值排序时的输出示例。

数组

$list = array (
        0 => array(
                0 => '<input class="id" name="id[]" type="checkbox" value="7">',
                1 => '7',
                2 => '17',
                3 => 'Group Name',
                4 => 'Katie-Lee',
                5 => 'Homes',
                6 => 'b@b.com'
        ),
        1 => array(
                0 => '<input class="id" name="id[]" type="checkbox" value="22">',
                1 => '22',
                2 => '17',
                3 => 'Group Name',
                4 => 'John',
                5 => 'Collins',
                6 => 'a@a.com',
        ),
        2 => array(
                0 => '<input class="id" name="id[]" type="checkbox" value="9">',
                1 => '9',
                2 => '17',
                3 => 'Group Name',
                4 => 'Rob',
                5 => 'Smith',
                6 => 'rob@smith.com',
        ),
        3 => array(
                0 => '<input class="id" name="id[]" type="checkbox" value="1">',
                1 => '1',
                2 => '17',
                3 => 'Group Name',
                4 => 'Claire',
                5 => 'Taylor',
                6 => 'claire.taylor@example.com',
        ),
);

按键排序 $list[][1] (id)

$list = array (
    0 => array(
                0 => '<input class="id" name="id[]" type="checkbox" value="1">',
                1 => '1', // <-- Sort by id
                2 => '17',
                3 => 'Group Name',
                4 => 'Claire',
                5 => 'Taylor',
                6 => 'claire.taylor@example.com',
        ),
    1 => array(
                0 => '<input class="id" name="id[]" type="checkbox" value="7">',
                1 => '7', // <-- Sort by id
                2 => '17',
                3 => 'Group Name',
                4 => 'Katie-Lee',
                5 => 'Homes',
                6 => 'b@b.com'
        ), 
    2 => array(
                0 => '<input class="id" name="id[]" type="checkbox" value="9">',
                1 => '9', // <-- Sort by id
                2 => '17',
                3 => 'Group Name',
                4 => 'Rob',
                5 => 'Smith',
                6 => 'rob@smith.com',
        ),
    3 => array(
                0 => '<input class="id" name="id[]" type="checkbox" value="22">',
                1 => '22', // <-- Sort by id
                2 => '17',
                3 => 'Group Name',
                4 => 'John',
                5 => 'Collins',
                6 => 'a@a.com',
        ), 
);

按键排序 $list[][6](电子邮件)

$list = array (
    0 => array(
                0 => '<input class="id" name="id[]" type="checkbox" value="22">',
                1 => '22',
                2 => '17',
                3 => 'Group Name',
                4 => 'John',
                5 => 'Collins',
                6 => 'a@a.com', // <-- Sort by email
        ),
        1 => array(
                0 => '<input class="id" name="id[]" type="checkbox" value="7">',
                1 => '7',
                2 => '17',
                3 => 'Group Name',
                4 => 'Katie-Lee',
                5 => 'Homes',
                6 => 'b@b.com' // <-- Sort by email
        ),
    2 => array(
                0 => '<input class="id" name="id[]" type="checkbox" value="1">',
                1 => '1',
                2 => '17',
                3 => 'Group Name',
                4 => 'Claire',
                5 => 'Taylor',
                6 => 'claire.taylor@example.com', // <-- Sort by email
        ), 
        3 => array(
                0 => '<input class="id" name="id[]" type="checkbox" value="9">',
                1 => '9',
                2 => '17',
                3 => 'Group Name',
                4 => 'Rob',
                5 => 'Smith',
                6 => 'rob@smith.com', // <-- Sort by email
        ),

);

任何帮助都会很棒,谢谢。

更新

我已经更新了下面的代码,以表明它对其他人有效。

$list = array (
        0 => array(
                0 => '<input class="id" name="id[]" type="checkbox" value="7">',
                1 => '7',
                2 => '17',
                3 => 'Group Name',
                4 => 'Katie-Lee',
                5 => 'Homes',
                6 => 'b@b.com'
        ),
        1 => array(
                0 => '<input class="id" name="id[]" type="checkbox" value="22">',
                1 => '22',
                2 => '17',
                3 => 'Group Name',
                4 => 'John',
                5 => 'Collins',
                6 => 'a@a.com',
        ),
        2 => array(
                0 => '<input class="id" name="id[]" type="checkbox" value="9">',
                1 => '9',
                2 => '17',
                3 => 'Group Name',
                4 => 'Rob',
                5 => 'Smith',
                6 => 'rob@smith.com',
        ),
        3 => array(
                0 => '<input class="id" name="id[]" type="checkbox" value="1">',
                1 => '1',
                2 => '17',
                3 => 'Group Name',
                4 => 'Claire',
                5 => 'Taylor',
                6 => 'claire.taylor@example.com',
        ),
);

echo 'Array before sort:';
print("<pre>" . print_r($list, true). "</pre>");

$sortField = 6; // the id 

usort($list, function($a, $b) use ($sortField) 
{
        return strnatcmp($a[$sortField], $b[$sortField]);
});

echo 'Array after sort:';
print("<pre>" . print_r($list, true). "</pre>");

【问题讨论】:

标签: php sorting multidimensional-array


【解决方案1】:

如果您使用的是 php 5.3+,则可以使用 usort 和闭包。

$sortField = 1; // the id 

usort($array, function($a, $b) use ($sortField) {
      return strnatcmp($a[$sortField], $b[$sortField]);
});

这样,您将能够根据 sortField 变量中指定的字段对数组进行排序。

【讨论】:

  • 我已经使用您的代码更新了我的问题,但它只返回“true”。有任何想法吗?谢谢
  • 我已经弄明白了 :) 感谢您的帮助
猜你喜欢
  • 2012-06-15
  • 2013-04-03
  • 1970-01-01
  • 2021-07-11
  • 2012-08-03
相关资源
最近更新 更多