如何排序数组以管理最高的字符串长度? [重复]

时间:2021-04-24 07:44:52

This question already has an answer here:

这个问题在这里已有答案:

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

您可以从此链接获得进一步的参考

How-to-sort-array-as-per-text-length-of-value?

#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

您可以从此链接获得进一步的参考

How-to-sort-array-as-per-text-length-of-value?