[英]Sort multidimensional array by values
我有多维数组,我想按每个数组中的值对其进行排序。
我还没有真正按照多维数组中的值对多维数组进行排序,我自己也不高兴。
我给出了下面的数组,并给出了按数组中的值排序时的输出示例。
数组
$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 5.3+,则可以使用usort和闭包。
$sortField = 1; // the id
usort($array, function($a, $b) use ($sortField) {
return strnatcmp($a[$sortField], $b[$sortField]);
});
这样,您将能够根据sortField变量中指定的字段对数组进行排序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.