2

I want create a three-level array in php, with example data for developing purposes, I have this:

$data = array(
array(1 => array("A ROW GREENS", array(
    "A1" => array("http://flexslider.woothemes.com/images/kitchen_adventurer_cheesecake_brownie.jpg"),
    "A2" => array("http://flexslider.woothemes.com/images/kitchen_adventurer_cheesecake_brownie.jpg"),
    "A3" => array("http://flexslider.woothemes.com/images/kitchen_adventurer_cheesecake_brownie.jpg")
))),
array(2 => array("A ROW BLUE",array(
    "A1" => array("http://flexslider.woothemes.com/images/kitchen_adventurer_cheesecake_brownie.jpg"),
    "A2" => array("http://flexslider.woothemes.com/images/kitchen_adventurer_cheesecake_brownie.jpg"),
    "A3" => array("http://flexslider.woothemes.com/images/kitchen_adventurer_cheesecake_brownie.jpg")
))),

);

How you can see, this is the structure:

Element:[ID -> Title][IDSubitem1->URL, IDSubitem2->URL2...]
Element:[ID -> Title][IDSubitem1->URL, IDSubitem2->URL2...]
Element:[ID -> Title][IDSubitem1->URL, IDSubitem2->URL2...]

I need to print a <ul> with the Titles (using id for identifier them) and print other <ul> tags for show or hide the subitems or the selected parent.

<ul id="parent">
    <li id="1">A ROW GREENS</li>
    <li id="2">A ROW BLUE</li>
</ul>

<ul id="child1">
    <li id="child1-A1">http://flexslider.woothemes.com/images/kitchen_adventurer_cheesecake_brownie.jpg</li>
    <li id="child1-A2">http://flexslider.woothemes.com/images/kitchen_adventurer_cheesecake_brownie.jpg</li>
    <li id="child1-A3">http://flexslider.woothemes.com/images/kitchen_adventurer_cheesecake_brownie.jpg</li>
</ul>

<ul id="child2">
    <li id="child2-A1">http://flexslider.woothemes.com/images/kitchen_adventurer_cheesecake_brownie.jpg</li>
    <li id="child2-A2">http://flexslider.woothemes.com/images/kitchen_adventurer_cheesecake_brownie.jpg</li>
    <li id="child2-A3">http://flexslider.woothemes.com/images/kitchen_adventurer_cheesecake_brownie.jpg</li>
</ul>
3
  • 1
    can you show what expected outcome you want? and what you have tried to achieve that? Commented Oct 5, 2016 at 19:34
  • Also your first input is seems wrong array(1 => "A ROW GREENS") => array( Commented Oct 5, 2016 at 19:40
  • I updated the desired result and change a little the data (before was wrong) Commented Oct 6, 2016 at 14:08

2 Answers 2

2

You can achieve your expected outcome like below:-

<?php
$data = array(
array(1 => array("A ROW GREENS", array(
    "A1" => array("http://flexslider.woothemes.com/images/kitchen_adventurer_cheesecake_brownie.jpg"),
    "A2" => array("http://flexslider.woothemes.com/images/kitchen_adventurer_cheesecake_brownie.jpg"),
    "A3" => array("http://flexslider.woothemes.com/images/kitchen_adventurer_cheesecake_brownie.jpg")
))),
array(2 => array("A ROW BLUE",array(
    "A1" => array("http://flexslider.woothemes.com/images/kitchen_adventurer_cheesecake_brownie.jpg"),
    "A2" => array("http://flexslider.woothemes.com/images/kitchen_adventurer_cheesecake_brownie.jpg"),
    "A3" => array("http://flexslider.woothemes.com/images/kitchen_adventurer_cheesecake_brownie.jpg")
))));

$parent_data = '';
$child_data = '';
foreach($data as $dat){
    foreach($dat as $key=>$da){
        $parent_data .="<li id ='".$key."'>".$da[0]."</li>";
        $child_data .="<ul id='child".$key."'>";
        foreach ($da[1] as $k=>$v){
             $child_data .="<li id='child".$key."-".$k."'>".$v[0]."</li>";
        }
        $child_data .="</ul>";
    }

}
?>
<ul id="parent"><?php echo $parent_data;?></ul><?php echo  $child_data;?>

Output:- https://eval.in/656522

Note:- The code will only work for the given array structure (element can be more, no problem), But if array structure is changed then code will not work.

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

Comments

0

Have you try to use 3 foreach()

foreach($data as $level1)
{
  foreach(level1 as $level2)
  {
    foreach(level2 as $level3)
    {

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.