I've following associative array titled $requests
:
Note : Following is the output of the statement print_r($requests);
Array
(
[0] => Array
(
[request_id] => 33
[is_seen] => 1
[message] =>
[friend_user_id] => 901
)
[1] => Array
(
[request_id] => 23
[is_seen] => 1
[message] =>
[friend_user_id] => 970
)
)
Now what I want to achieve is I'll take value from user in a variable say $friend_user_id
. Now I've to parse through the above array for checking whether any of the inner array has got the same value as of $friend_user_id
in a key titled ['friend_user_id']
. If the match is found then the value from key ['request_id']
from the same inner array should be fetched, if no match is found then it should return nothing.
For example, the variable $friend_user_id
contains value 901 then it should return me 33(the respective request_id)
How should I achieve this in an efficient, optimum and reliable way?
Thanks in advance.
A simple foreach
along with in_array
will do the trick as
$friend_user_id = 901;
foreach($arr as $key => $value){
if(in_array($friend_user_id,$value)){
$request_id = $value['request_id'];
}
}
echo $request_id;
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments