...

View Full Version : Checking if a value appears in an array



thesavior
07-04-2007, 06:31 PM
So I have this array which contains an array which contains values. I want to check if the value provided is inside the second array.

Here is my structure:



$array=array(
"ARRAY2"=>array(
"Info",
"Board",
"License",
),
"ARRAY3"=>array(
"Structure",
"Edit",
"Add",
"Delete",
),
"ARRAY4"=>array(
"View",
"Search",
"Groups",
"Delete",
),
"ARRAY5"=>array(
"Main",
"Themes",
"Groups",
),
);


lets say I want to check if Groups is inside ARRAY 4. How would I do that? I know how to check if ARRAY 4 is inside array, but if you go down a level im not exactly sure how to do that.

Leppy
07-04-2007, 07:02 PM
With 2 minutes of thinking I would do this


<?php
$array=array(
"ARRAY2"=>array(
"Info",
"Board",
"License",
),
"ARRAY3"=>array(
"Structure",
"Edit",
"Add",
"Delete",
),
"ARRAY4"=>array(
"View",
"Search",
"Groups",
"Delete",
),
"ARRAY5"=>array(
"Main",
"Themes",
"Groups",
),
);

foreach ($array AS $key => $val_array){
if(in_array('Groups',$val_array)){
// do something!
echo "Found it in array['{$key}']!<br>";
}
}
?>

That's for checking all arrays but if you want know which key like ARRAY4 for example, you just have to use this syntax


in_array("Groups",$array['ARRAY4']);

thesavior
07-04-2007, 08:02 PM
Heh, thanks, I forgot that you can call array inside an array by $array['ARRAY4']. That solved my problem.

Leppy
07-04-2007, 08:09 PM
Glad that helped :) At first I wasn't sure if that would do it hehe

thesavior
07-04-2007, 08:17 PM
heh, yep.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum