Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-04-2007, 06:31 PM   PM User | #1
thesavior
Senior Coder

 
Join Date: Aug 2005
Posts: 1,119
Thanks: 2
Thanked 1 Time in 1 Post
thesavior has a little shameless behaviour in the past
Checking if a value appears in an array

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:

PHP Code:
$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.
thesavior is offline   Reply With Quote
Old 07-04-2007, 07:02 PM   PM User | #2
Leppy
New Coder

 
Join Date: Jul 2007
Posts: 26
Thanks: 1
Thanked 0 Times in 0 Posts
Leppy is an unknown quantity at this point
With 2 minutes of thinking I would do this

PHP Code:
<?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

PHP Code:
in_array("Groups",$array['ARRAY4']); 
__________________
-
Leppy is offline   Reply With Quote
Old 07-04-2007, 08:02 PM   PM User | #3
thesavior
Senior Coder

 
Join Date: Aug 2005
Posts: 1,119
Thanks: 2
Thanked 1 Time in 1 Post
thesavior has a little shameless behaviour in the past
Heh, thanks, I forgot that you can call array inside an array by $array['ARRAY4']. That solved my problem.
thesavior is offline   Reply With Quote
Old 07-04-2007, 08:09 PM   PM User | #4
Leppy
New Coder

 
Join Date: Jul 2007
Posts: 26
Thanks: 1
Thanked 0 Times in 0 Posts
Leppy is an unknown quantity at this point
Glad that helped At first I wasn't sure if that would do it hehe
__________________
-
Leppy is offline   Reply With Quote
Old 07-04-2007, 08:17 PM   PM User | #5
thesavior
Senior Coder

 
Join Date: Aug 2005
Posts: 1,119
Thanks: 2
Thanked 1 Time in 1 Post
thesavior has a little shameless behaviour in the past
heh, yep.
thesavior is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:19 PM.


Advertisement
Log in to turn off these ads.