This question already has an answer here:
这个问题在这里已有答案:
- PHP: Sort an array by the length of its values? 8 answers
PHP:按照值的长度对数组进行排序? 8个答案
I am new in php and i have an array like below
我是php的新手,我有一个如下所示的数组
$sv = array(
"my name is john", // length 15
"i am living in london from my childwood", // length 39
"i am 13 years old", // length 17
"i like to become software engineer in future" // length 44
);
Now i want to try to keep highest length value on top position in my array and lowest in the bottom of the array.
现在我想尝试在数组的顶部位置保持最高长度值,在数组底部保持最低值。
and my output will be
我的输出将是
Array
(
[0] => i like to become software engineer in future
[1] => i am living in london from my childwood
[2] => i am 13 years old
[3] => my name is john
)
If anyone can know the answer then please share it.
如果有人能够知道答案,请分享。
1 个解决方案
#1
I think you should try
我想你应该试试
usort($sv, create_function('$a,$b','return strlen($b) - strlen($a);'));
You can get further reference from this link
您可以从此链接获得进一步的参考
#1
I think you should try
我想你应该试试
usort($sv, create_function('$a,$b','return strlen($b) - strlen($a);'));
You can get further reference from this link
您可以从此链接获得进一步的参考