note 113156 deleted from function.array-search by levim

From: Date: Mon, 24 Feb 2014 22:45:59 +0000
Subject: note 113156 deleted from function.array-search by levim
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-198661@lists.php.net to get a copy of this message
Note Submitter: mraedit at yahoo dot com ---- Here example if you need to search in multidimensional array for specific value but the return value is another value from arry this use when get rows from table in DB and you need to search in it <?php function get_value_from_array($needle, $arr, $key_of_search, $name_of_key_to_return_value) { if (is_array($arr)) { for($i=0; $i<sizeof($arr); $i++) { $row = $arr[$i]; if($row[$key_in_arr] === $needle ) return $row[$name_of_key_to_return_value]; } } return false; } ?> Testing : I want to get last name of kasem Array ( [0] => Array ( [id] => 1 [first_name] => 'Ahmad' [last_name] => 'Mohammad' ) [0] => Array ( [id] => 2 [first_name] => 'Kasem' [last_name] => 'Maged' ) ) get_value_from_array('Kasem', $arr, 'first_name', 'last_name') The result is : Maged

« previous php.notes (#198661) next »