我有多维数组,我试图按每个数组中的值对其进行排序。
我并没有真正按其中的值对多维数组进行排序,我自己也没有快乐。
我给出了下面的数组和按数组中的值排序时的输出示例。
大批
$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] (email)
$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>");