?
Solved

help with multi dem array in_array

Posted on 2006-04-22
3
Medium Priority
?
358 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
        )
)
0
Comment
Question by:qwertq
  • 2
3 Comments
 
LVL 17

Accepted Solution

by:
BogoJoker earned 800 total points
ID: 16517226
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
0
 

Author Comment

by:qwertq
ID: 16517246
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;
}
0
 
LVL 17

Expert Comment

by:BogoJoker
ID: 16517254
Great manipulation, glad you were flexable with my suggestion =)
Keep it up!
Joe P
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

609 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question