note 71120 deleted from function.usort by cmb

From: Date: Sun, 13 Oct 2019 08:12:05 +0000
Subject: note 71120 deleted from function.usort by cmb
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-212625@lists.php.net to get a copy of this message
Note Submitter: Tim ---- If you want to sort an array of strings by the length of its values, you can write a better cmp-function than manually comparing string lengths with if-statements. Unfortunately, I see this bad technique in other people's code. I prefer using this: <?php function length_cmp( $a, $b ) { return strlen($a)-strlen($b) ; } ?> This function sorts the array in ascending order. If you want to sort descending, simply swap $a and $b either in the parameters or in the subtraction. -Tim

« previous php.notes (#212625) next »