note 104649 deleted from function.usort by crell

From: Date: Wed, 30 Mar 2022 22:14:24 +0000
Subject: note 104649 deleted from function.usort by crell
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-217816@lists.php.net to get a copy of this message
Note Submitter: bcsj ---- Sorting a multidimensional array by a subkey seem to be one of the bigger challenges when using usort. At least if one want the key to be dynamic. For anyone interested this code seemed to work rather well for me: <?php usort($array,array(new cmp($key), "cmp__")); class cmp { var $key; function __construct($key) { $this->key = $key; } function cmp__($a,$b) { $key = $this->key; if ($a[$key] == $b[$key]) return 0; return (($a[$key] > $b[$key]) ? 1 : -1); } } ?>

« previous php.notes (#217816) next »