0

I am trying to print sub elements. Following this example. How to do it?.

$myarray = array("DO"=>array('IDEAS','BRANDS','CREATIVE','CAMPAIGNS'),
"JOCKEY"=>array('IDEAS','BRANDS','CREATIVE','CAMPAIGNS'),
"CREATE"=>array('IDEAS','BRANDS','CREATIVE','CAMPAIGNS'),
"INNOVATE"=>array('IDEAS','BRANDS','CREATIVE','CAMPAIGNS')); 

foreach($myarray as $key => $element){
    echo "<span class='rotate'>$element</span>";
    foreach($element as $subkey => $subelement){
        $out .= "$subelement";
        echo "<span class='rotate'>$out</span>";
    }
}
0

3 Answers 3

1

You need to do like below:-

<?php
$myarray = array("DO"=>array('IDEAS','BRANDS','CREATIVE','CAMPAIGNS'),
"JOCKEY"=>array('IDEAS','BRANDS','CREATIVE','CAMPAIGNS'),
"CREATE"=>array('IDEAS','BRANDS','CREATIVE','CAMPAIGNS'),
"INNOVATE"=>array('IDEAS','BRANDS','CREATIVE','CAMPAIGNS')); 
$out = ''; // define the out variable
foreach($myarray as $key => $element){
    echo "<span class='rotate'>$key</span>.<br><br>"; // print the indexes of main array
    foreach($element as $subkey => $subelement){
        $out .= "$subelement";
        echo "<span class='rotate'>$out</span><br>"; // print the sub elements present in the form of array at each indexes of the main array 
    }
}
?>

Output:- http://prntscr.com/7mribs

Sign up to request clarification or add additional context in comments.

1 Comment

Why should the OP "do like below"? Please add an explanation of what you did and why you did it that way, not only for the OP but for future visitors to SO. We've had this conversation before.
0
<?php<br/>
$cars = array<br/>
   (<br/>
   array("Volvo",22,18),<br/>
   array("BMW",15,13),<br/>
   array("Saab",5,2),<br/>
   array("Land Rover",17,15)<br/>
   );<br/>

for ($row = 0; $row <  4; $row++) {<br/>
   echo "<p><b>Row number $row</b></p>";<br/>
   echo "<ul>";<br/>
   for ($col = 0; $col <  3; $col++) {<br/>
     echo "<li>".$cars[$row][$col]."</li>";<br/>
   }<br/>
   echo "</ul>";<br/>
}<br/>
?>

1 Comment

Why should the OP try this? Good answers have an explanation of what was done and why it was done that way, not only for the OP but for future visitors to SO.
0

The link you have provided is not working. Judging from your post i looks like you want to print the index and the sub items below it, so this snippet does exactly that:

<?php
$myarray = array("DO"=>array('IDEAS','BRANDS','CREATIVE','CAMPAIGNS'),
"JOCKEY"=>array('IDEAS','BRANDS','CREATIVE','CAMPAIGNS'),
"CREATE"=>array('IDEAS','BRANDS','CREATIVE','CAMPAIGNS'),
"INNOVATE"=>array('IDEAS','BRANDS','CREATIVE','CAMPAIGNS')); 

foreach($myarray as $index => $s)
{
    echo "<ul>";
    echo "<li>" . $index . "<ul>";

    foreach($s as $sub)
    {
        echo "<li>" . $sub . "</li>";
    }

    echo "</ul></li></ul>";

}
?>

2 Comments

Why are you "guessing"? Why should the OP try this? Good answers have an explanation of what was done and why it was done that way, not only for the OP but for future visitors to SO.
Because the description is not detailed enough, ergo: guess this is what the OP is looking for, if its not, he can update his question and ill update mine to present another method.

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.