note 40932 added to function.in-array

From: Date: Tue, 23 Mar 2004 18:03:41 +0000
Subject: note 40932 added to function.in-array
Groups: php.notes 
Request: Send a blank email to php-notes+get-67068@lists.php.net to get a copy of this message
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); ?> ---- Manual Page -- http://www.php.net/manual/en/function.in-array.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+40932 Delete -- http://master.php.net/manage/user-notes.php?action=delete+40932&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+40932&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#67068) next »