0

I have a 3 dimensional array defined like:

$seccc  =  array(   
                                array( 
                                        "Href"      => base_url().'capture/',   
                                        "Icono"     => base_url().'assets/images/icon_home.png', 
                                        "Texto"     => 'Captura',
                                        "Submenu"   => array(1,2,3)
                                    ),
                                array( 
                                        "Href"      => base_url().'seg/',   
                                        "Icono"     => base_url().'assets/images/icon_tra.png', 
                                        "Texto"     => 'Seg',
                                        "Submenu"   => array('ALFA','OMEGAL','DELTA')
                                    ),
                                array( 
                                        "Href"      => base_url().'usr/',   
                                        "Icono"     => base_url().'assets/images/icon_users.png', 
                                        "Texto"     => 'Users',
                                        "Submenu"   => ''
                                    ),  
                                array( 
                                        "Href"      => base_url().'clients/',   
                                        "Icono"     => base_url().'assets/images/icono_gro.png', 
                                        "Texto"     => 'Clients',
                                        "Submenu"   => ''
                                    ),
                                array( 
                                        "Href"      => base_url().'suc/',   
                                        "Icono"     => base_url().'assets/images/icupo.png', 
                                        "Texto"     => 'Suc',
                                        "Submenu"   => ''
                                    )
                            );

I am doing foreach loop like

foreach ($seccc  as $part)
    {
        foreach ($part as $item)
        {
            echo '<li>'.$item["Href"];
            if(is_array($item["Submenu"]))
            {
               foreach($item["Submenu"] as $subkey)
               {
                echo '<ul>';
                echo $subkey;                
                echo '</ul>';
               }                    
            }
        }
        echo '</li>';            
        }
    }

However I can not access to "Href", "Icono", "Texto" or "Submenu" items, How to access their values seems $item["Href"] does not work

1
  • given the structure above, you are doing it correct. I would do a print_r of the array after building it to make sure the output is what you expect. Feel free to post that print_r output and someone can help if needed. Commented Nov 21, 2014 at 18:47

2 Answers 2

1
foreach ($seccc as $part)
{
    // use $part instead of $item, here you can get $part['Icono'], $part['Texto'] etc
    echo '<li>'.$part["Href"]; 
    if(is_array($part["Submenu"])) 
    {
        // loop over $part['Submenu'] if it's an array
        foreach($part["Submenu"] as $key => $subkey)
        {
            echo '<ul>';
            echo $subkey;                
            echo '</ul>';
        }                    
    }
    echo '</li>';            
}
Sign up to request clarification or add additional context in comments.

4 Comments

for some reason I am getting repeated correct display like each part is being repeated 4 times...
@thecodeparadox You were using a useless loop I'm glad you updated it.
One last question, if I woul want to loop all items inside $part["Submenu"] (in case it is an array) how should I do it?
@cMinor Already updated the answer how to loop over $part['Submene'].
1

You have one loop to many

foreach ($seccc as $item)
{
  echo '<li>'.$item["Href"];
  if(is_array($item["Submenu"]))
  {
     foreach($item["Submenu"] as $subkey)
     {
      echo '<ul>';
      echo $subkey;                
      echo '</ul>';
     }                    
  }    
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.