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条)

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能