Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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 :)

share|improve this question
1  
Please format your print_r next time. –  Layke Jul 22 '11 at 12:11

3 Answers

up vote 2 down vote accepted
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.

share|improve this answer
 
Hi, Thanks for your response. –  user469453 Jul 22 '11 at 12:14
 
Hi, Many thanks for your help. Works perfectly :) –  user469453 Jul 22 '11 at 12:20
 
@bazmegakapa: Needed a check for existance of $key in $h. (Stop assuming!) Edited your snippet. –  nikc.org Jul 22 '11 at 12:21
 
@nikc Well, thanks. If you have used the comments to do this, I would have edited it into my question :). –  bažmegakapa Jul 22 '11 at 12:26
 
@bazmegakapa: Thought I'd save you the trouble ;-) –  nikc.org Jul 22 '11 at 12:35

Write a method something like this:

function valInArr($array, $field, $value)
 foreach ($array as $id => $nestedArray) {
  if (strpos($value,$nestedArray[$field])) return $id;
  //if ($nestedArray[$field] === $value) return $id; // use this line if you want the values to be identical
 }
 return false;
}

$meetID = 1234;
$x = valInArr($array, "Meet_Name", $meetID);
if ($x) print_r($array[$x]);

This function will evaluate true if the record is found in the array and also enable you to quickly access the specific nested array matching that ID.

share|improve this answer

For single-dimensional arrays you can use array_search(). This can be adapted for multi-dimensional arrays like so:

function array_search_recursive($needle, $haystack, $strict=false, $stack=array()) {
    $results = array();
    foreach($haystack as $key=>$value) {
        if(($strict && $needle === $value) || (!$strict && $needle == $value)) {
            $results[] = array_merge($stack, array($key));
        }

        if(is_array($value) && count($value) != 0) {
            $results = array_merge($results, array_search_recursive($needle, $value, $strict, array_merge($stack, array($key))));
        }
    }
    return($results);
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.