note 37395 added to function.uksort

From: Date: Thu, 13 Nov 2003 15:59:13 +0000
Subject: note 37395 added to function.uksort
Groups: php.notes 
Request: Send a blank email to php-notes+get-60336@lists.php.net to get a copy of this message
To use a more complicated comparison function, one can use a callback to a method of an object instance. For example the following will take an array $arr whose keys are the same as those of $reference, and reorder $arr so that the keys appear in the same order as in $reference. class kcmp { var $reference ; function kcmp( $reference ) { $this->reference = $reference ; } function kcompare( $a, $b ) { $keys = array_keys( $this->reference ) ; $position_a = array_search( $a, $keys ) ; $position_b = array_search( $b, $keys ) ; return $position_a < $position_b ? -1 : 1 ; } } $reference = array( "k2" => "a2", "k3" => "a3", "k1" => "a1" ) ; $arr = array( "k1" => "b1", "k2" => "b2", "k3" => "b3" ) ; print_r( $arr ) ; uksort( $arr, array( new kcmp( $reference ), "kcompare" ) ) ; print_r( $arr ) ; ---- Manual Page -- http://www.php.net/manual/en/function.uksort.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+37395 Delete -- http://master.php.net/manage/user-notes.php?action=delete+37395&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+37395&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#60336) next »