note 42101 deleted from function.array-search by levim

From: Date: Mon, 24 Feb 2014 22:37:30 +0000
Subject: note 42101 deleted from function.array-search by levim
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-198642@lists.php.net to get a copy of this message
Note Submitter: softexpert [at] libertysurf [dot] fr ---- I use this for searching for a value in a bidimensional array . <?php function SearchBiDimArray(&$theArray, $dimNo, $searchValue, $returnIndex = true){ if(is_array($theArray)){ $keys = array_keys($theArray[0]); $key = $keys[$dimNo]; $elcount = count($theArray); for($i=0; $i < $elcount; $i++){ if($theArray[$i][$key] === $searchValue){ if ($returnIndex){ return $i; } else{ return $theArray[$i]; } } } } else{ return array_search($searchValue, $theArray); } } $theArray = array(); $theArray[0]['firstproperty'] = 'avalue1'; $theArray[0]['secondproperty'] = 'anothervalue1'; $theArray[1]['firstproperty'] = 'avalue2'; $theArray[1]['secondproperty'] = 'anothervalue2'; $theArray[2]['firstproperty'] = 'avalue3'; $theArray[2]['secondproperty'] = 'anothervalue3'; print SearchBiDimArray($theArray, 1, 'anothervalue2', true); // result is 1 print SearchBiDimArray($theArray, 1, 'anothervalue2', true); // result is //Array //( // [firstproperty] => avalue2 // [secondproperty] => anothervalue2 //) ?>

« previous php.notes (#198642) next »