我有多维数组,我正在尝试按每个数组中的值对其进行排序。
我没有真正按照其中的值对多维数组进行排序,而且我自己也没有快乐。
我在下面给出了数组,并在数组中按值排序时输出示例。
阵列
$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>");
答案 0 :(得分:0)
如果你使用的是php 5.3+,你可以使用usort和一个闭包。
$sortField = 1; // the id
usort($array, function($a, $b) use ($sortField) {
return strnatcmp($a[$sortField], $b[$sortField]);
});
通过这种方式,您将能够根据sortField变量中指定的字段对数组进行排序。