note 103644 added to function.array-search

From: Date: Tue, 26 Apr 2011 03:17:38 +0000
Subject: note 103644 added to function.array-search
Groups: php.notes 
Request: Send a blank email to php-notes+get-179131@lists.php.net to get a copy of this message
This is a function that I wrote which works like the search array function, it searches the array for a given value and returns the corresponding key if successful. But this one goes one deeper in the case an array also contains one or many other array. example: $letters_map = array( '2' => array('a', 'b', 'c'), '3' => array('d', 'e', 'f'), '4' => array('g', 'h', 'i'), '5' => array('j', 'k', 'l'), '6' => array('m', 'n', 'o'), '7' => array('p', 'q', 'r', 's'), '8' => array('t', 'u', 'v'), '9' => array('w', 'x', 'y', 'z') ); multi_array_search('n', $letters_map); //returns 6 function multi_array_search($needle, $haystackArray){ $Key; foreach($haystackArray as $key => $value){ if(is_array($value)){ $value = array_search($needle, $value); if(!empty($value)){//needle is found $Key = $key; break; } } } return $Key; } ---- Server IP: 192.41.42.26 Probable Submitter: 71.35.17.108 ---- Manual Page -- http://www.php.net/manual/en/function.array-search.php Edit -- https://master.php.net/note/edit/103644 Del: integrated -- https://master.php.net/note/delete/103644/integrated Del: useless -- https://master.php.net/note/delete/103644/useless Del: bad code -- https://master.php.net/note/delete/103644/bad+code Del: spam -- https://master.php.net/note/delete/103644/spam Del: non-english -- https://master.php.net/note/delete/103644/non-english Del: in docs -- https://master.php.net/note/delete/103644/in+docs Del: other reasons-- https://master.php.net/note/delete/103644 Reject -- https://master.php.net/note/reject/103644 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#179131) next »