We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

help with multi dem array in_array

qwertq
qwertq asked
on
Medium Priority
372 Views
Last Modified: 2006-11-18
i am using this to test multi dem arrays
function in_array_multidem($array, $val){
    if(!is_array($array)){
        return false;
    }

    if(in_array($val, $array)){
        return true;
    }

    foreach($array as $row){
        if(array_multi_search($val, $array )){
            return true;
        }
    }
    return false;
}


but i want to change it slightly. i would like to supply an additional parameter to restict it to a named array value.
in the array below i would like to test if [foo] = apple (which should return false) but right now it will return true because technically apple is in_array

Array
(
    [0] => Array
        (
            [foo] => orange
            [bar] => apple
        )
)
Comment
Watch Question

Hi qwertq,

Well, you can check if it isset, if it is, then check its value.

foreach($array as $row)
{
    if (isset($row['foo']))
    {
        if ($row['foo'] == 'apple')
          return true;
    }
}
return false;

How about that?
Joe P

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
thanks, that worked.

here is my updated function for future reference

function in_array_multidem($array, $val, $node = false){

    if(!is_array($array)){
        return false;
    }

    if(in_array($val, $array)){
        return true;
    }
   
    foreach($array as $row){
       
        if ($node && isset($row[$node])){
            if ($row[$node] == $val){
                return true;
            }
        }
           
        if(array_multi_search($val, $array )){
            return true;
        }
    }
    return false;
}
Great manipulation, glad you were flexable with my suggestion =)
Keep it up!
Joe P
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.