note 40932 deleted from function.in-array by felipe

From: Date: Sun, 30 Dec 2007 18:53:19 +0000
Subject: note 40932 deleted from function.in-array by felipe
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-134475@lists.php.net to get a copy of this message
Note Submitter: fierojoe at fierojoe dot com ---- I couldn't find an easy way to search an array of associative arrays and return the key of the found associative array, so I created this little function to do it for me. Very handy for searching through the results of a mysql_fetch_assoc() <?php $a = array( array('id' => 1, 'name' => 'oranges'), array('id' => 2, 'name' => 'apples'), array('id' => 3, 'name' => 'coconuts'), ); function _array_search($s, $a) { for($i=0; $i <= count($a); $i++) { if (in_array($s, $a[$i])) { return($i); break; } } return(FALSE); } //returns key 0 for the second element, 'id' => 1 echo _array_search(1, $a); //returns key 2 for the second element, 'id' => 3 echo _array_search(3, $a); //returns key 1 for the second element, 'id' => 2 echo _array_search(2, $a); ?>

« previous php.notes (#134475) next »