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

on
Medium Priority
372 Views
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

## View Solution Only

Commented:
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;

Joe P

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

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;
}

Commented:
Great manipulation, glad you were flexable with my suggestion =)
Keep it up!
Joe P
##### Thanks for using Experts Exchange.

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