note 99719 deleted from function.array-multisort by danbrown

From: Date: Wed, 01 Sep 2010 14:41:21 +0000
Subject: note 99719 deleted from function.array-multisort by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-172236@lists.php.net to get a copy of this message
Note Submitter: Ricardo Sismeiro ---- <?php $t = array(); $t[] = array('m'=>1,'t'=>'a','d'=>array('k','k','k')); $t[] = array('m'=>4,'t'=>'e','d'=>array('w','w','w')); $t[] = array('m'=>3,'t'=>'d','d'=>array('h','h','h')); $t[] = array('m'=>2,'t'=>'c','d'=>array('e','e','e')); $t[] = array('m'=>5,'t'=>'b','d'=>array('i','i','i')); function sort_by($array,$field,$desc='desc'){ $index = array(); $result = array(); foreach($array as $k=>$row){ $index[] = $row[$field];} if ('desc' == $desc){ arsort($index); } else { asort($index);} foreach($index as $k=>$value){ $result[] = $array[$k]; unset($array[$k]); } return $result; } $m1 = sort_by($t, 'm'); $m2 = sort_by($t, 't'); print_r($m1); print_r($m2); ?>

« previous php.notes (#172236) next »