douhan4243 2017-08-08 12:39
浏览 86

对数组的元素进行排序[重复]

This question already has an answer here:

I have an array like the following:

$a= $array('PHP','HTML','JS','LARAVEL');

I want to sort the elements in the array, descending by the total number of characters of an element

$b= $array('LARAVEL','HTML','PHP','JS');

Please help me to descend the elements of the array, based on the number of characters in an array.

</div>
  • 写回答

2条回答 默认 最新

  • dongmen5867 2017-08-08 12:42
    关注

    You need to use usort():

    function sort($a,$b){
        return strlen($b)-strlen($a);
    }
    $array = ['PHP','HTML','JS','LARAVEL'];
    usort($array,'sort');
    
    评论

报告相同问题?