Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP find value in multidimensional / nested array

I've trawled the site and the net and have tried various recursive functions etc to no avail, so I'm hoping someone here can point out where I'm going wrong :)

I have an array named $meetingArray with the following values;

Array ( 
    [0] => Array ( 
        [Meet_ID] => 9313 
        [Meet_Name] => 456136 
        [Meet_CallInNumber] => 
        [Meet_AttendeeCode] => 
        [Meet_Password] => 
        [Meet_ScheduledDateTime] => 2011-07-18 16:00:00 
        [Meet_ModeratorCode] => 
        [Meet_RequireRegistration] => 0 
        [Meet_CurrentUsers] => 0 
    ) 
    [1] => Array ( 
        [Meet_ID] => 9314 
        [Meet_Name] => 456120 
        [Meet_CallInNumber] => 
        [Meet_AttendeeCode] => 
        [Meet_Password] => 
        [Meet_ScheduledDateTime] => 2011-07-18 16:00:00 
        [Meet_ModeratorCode] => 
        [Meet_RequireRegistration] => 0 
        [Meet_CurrentUsers] => 0 
    ) 
)

I also have a variable named $meetID.

I want to know if the value in $meetID appears in [Meet_Name] within the array and simply evaluate this true or false.

Any help very much appreciated before I shoot myself :)

like image 389
user469453 Avatar asked Jul 22 '11 12:07

user469453


1 Answers

function multi_in_array($needle, $haystack, $key) {
    foreach ($haystack as $h) {
        if (array_key_exists($key, $h) && $h[$key]==$needle) {
            return true;
        }
    }
    return false;
}

if (multi_in_array($meetID, $meetingArray, 'Meet_Name')) {
    //...
}

I am unsure what you mean by

$meetID appears in [Meet_Name]

but simply substitute the $h[$key]==$needle condition with something that meets your needs.

like image 135
kapa Avatar answered Nov 03 '22 02:11

kapa