note 107081 deleted from function.sort by danbrown
| From: | danbrown@php.net | Date: | Fri, 06 Jan 2012 19:06:19 +0000 |
| Subject: | note 107081 deleted from function.sort by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-185397@lists.php.net to get a copy of this message | ||
Note Submitter: jazzduck at nospam dot gmail dot com
----
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!