Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Senior Coder
    Join Date
    Aug 2005
    Posts
    1,119
    Thanks
    2
    Thanked 1 Time in 1 Post

    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.

  • #2
    New Coder
    Join Date
    Jul 2007
    Posts
    26
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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']); 
    -

  • #3
    Senior Coder
    Join Date
    Aug 2005
    Posts
    1,119
    Thanks
    2
    Thanked 1 Time in 1 Post
    Heh, thanks, I forgot that you can call array inside an array by $array['ARRAY4']. That solved my problem.

  • #4
    New Coder
    Join Date
    Jul 2007
    Posts
    26
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Glad that helped At first I wasn't sure if that would do it hehe
    -

  • #5
    Senior Coder
    Join Date
    Aug 2005
    Posts
    1,119
    Thanks
    2
    Thanked 1 Time in 1 Post
    heh, yep.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •