note 93106 modified in function.usort by danbrown

From: Date: Mon, 24 Aug 2009 17:08:58 +0000
Subject: note 93106 modified in function.usort by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-159838@lists.php.net to get a copy of this message
With php 5.3.0 sorting by properties of an object becomes MUCH simpler. <?php function osort(&$array, $prop) { usort($array, function($a, $b) use ($prop) { return $a->$prop > $b->$prop ? 1 : -1; }); } ?> Note that this uses anonymous functions / closures. Might find reviewing the php docs on that useful. Look below for examples for previous version of php. --was-- With php 5.3.0 sorting by properties of an object becomes MUCH simpler. function osort(&$array, $prop) { usort($array, function($a, $b) use ($prop) { return $a->$prop > $b->$prop ? 1 : -1; }); } Note that this uses anonymous functions / closures. Might find reviewing the php docs on that useful. Look below for examples for previous version of php. http://php.net/manual/en/function.usort.php

« previous php.notes (#159838) next »