【发布时间】: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