note 107081 added to function.sort
| From: | jazzduck at nospam dot gmail dot com | Date: | Thu, 05 Jan 2012 16:45:36 +0000 |
| Subject: | note 107081 added to function.sort | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-185376@lists.php.net to get a copy of this message | ||
Holdoffhunger was looking for a way to sort strings by their length. Here is a much smaller and less
roundabout way:
<?php
usort( $array_of_data, function( $x, $y ){ return strlen( $x ) - strlen( $y ); } );
?>
This will sort shortest-to-longest; if you want longest-to-shortest then reverse the equation:
strlen( $y ) - strlen( $x ).
Requires PHP 5.3 or greater!
----
Server IP: 208.69.120.55
Probable Submitter: 67.173.79.61
----
Manual Page -- http://www.php.net/manual/en/function.sort.php
Edit -- https://master.php.net/note/edit/107081
Del: integrated -- https://master.php.net/note/delete/107081/integrated
Del: useless -- https://master.php.net/note/delete/107081/useless
Del: bad code -- https://master.php.net/note/delete/107081/bad+code
Del: spam -- https://master.php.net/note/delete/107081/spam
Del: non-english -- https://master.php.net/note/delete/107081/non-english
Del: in docs -- https://master.php.net/note/delete/107081/in+docs
Del: other reasons-- https://master.php.net/note/delete/107081
Reject -- https://master.php.net/note/reject/107081
Search -- https://master.php.net/manage/user-notes.php