我有一个字符串数组,我想按照长度递减的顺序排序。但是,它是一个二维数组,我不知道如何实现一个能满足我想要的功能。
以下是数组的示例:
Array
(
[0] => Array
(
[0] => "abc"
[1] => "def"
[2] => "1234"
)
[1] => Array
(
[0] => "ghijkl"
[1] => "092"
[2] => "234234"
)
[2] => Array
(
[0] => "mn"
[1] => "opq"
[2] => "67843"
)
)
我只对子数组中第一项的长度感兴趣。理想情况下,最终会看起来像这样:
Array
(
[0] => Array
(
[0] => "ghijkl"
[1] => "092"
[2] => "234234"
)
[1] => Array
(
[0] => "abc"
[1] => "def"
[2] => "1234"
)
[2] => Array
(
[0] => "mn"
[1] => "opq"
[2] => "67843"
)
)
欢呼,你可以给我任何帮助。
答案 0 :(得分:5)
usort($array, function ($a, $b) { return strlen($b[0]) - strlen($a[0]); });
(使用PHP 5.3语法)
答案 1 :(得分:1)
<?php
$your_array = array
(
array
(
"abc",
"def",
"1234"
),
array
(
"ghijkl",
"092",
"234234"
),
array
(
"mn",
"opq",
"67843"
)
);
function customSort(Array $a, Array $b){
return strlen($b[0]) - strlen($a[0]);
}
usort($your_array, 'customSort');
var_dump($your_array);