PHP中的3级数组以组织数据 [英] 3 level array in php to organize data

查看:138
本文介绍了PHP中的3级数组以组织数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在php中创建一个三级数组,并带有用于开发目的的示例数据,我有:

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")
))),

);

您将看到以下结构:

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

我需要打印带有标题的<ul>(使用id标识它们),并打印其他<ul>标签以显示或隐藏子项或选定的父项.

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>

推荐答案

您可以实现预期的结果,如下所示:-

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;?>

输出:- 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.

这篇关于PHP中的3级数组以组织数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆