doumao9363 2016-10-05 19:30
浏览 26
已采纳

php中的3级数组来组织数据[关闭]

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>
  • 写回答

2条回答 默认 最新

  • dqjmq28248 2016-10-06 16:03
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题