note 113156 added to function.array-search
| From: | mraedit at yahoo dot com | Date: | Fri, 06 Sep 2013 07:41:37 +0000 |
| Subject: | note 113156 added to function.array-search | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-196181@lists.php.net to get a copy of this message | ||
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
----
Server IP: 69.147.83.199
Probable Submitter: 5.57.6.10 (proxied: 78.108.164.227)
----
Manual Page -- http://php.net/manual/en/function.array-search.php
Edit -- https://master.php.net/note/edit/113156
Del: integrated -- https://master.php.net/note/delete/113156/integrated
Del: useless -- https://master.php.net/note/delete/113156/useless
Del: bad code -- https://master.php.net/note/delete/113156/bad+code
Del: spam -- https://master.php.net/note/delete/113156/spam
Del: non-english -- https://master.php.net/note/delete/113156/non-english
Del: in docs -- https://master.php.net/note/delete/113156/in+docs
Del: other reasons-- https://master.php.net/note/delete/113156
Reject -- https://master.php.net/note/reject/113156
Search -- https://master.php.net/manage/user-notes.php