Home >Backend Development >PHP Tutorial >使用PHP架构嵌套数组,在线等

使用PHP架构嵌套数组,在线等

WBOY
WBOYOriginal
2016-06-23 14:05:55933browse

想架构一个嵌套数组,以输出JSON供前台页面级联菜单使用,但始终无法实现,求教。
大致结构如下:
商品[commod]
  |-饮料类[drink](clsID,clsName)
  |    |-碳酸饮料[sodas](itemID,itemName)
  |    |-果汁饮料[juice](itemID,itemName)
  |
  |-食品类[food](clsID,clsName)
       |-生鲜[fresh](itemID,itemName)
       |-熟食[cooked](itemID,itemName)


原先写了一个,但没有出来效果

<?php    /**/    $tmparr[] = array('ClsID'=>'01','ClsName'=>'饮料类');    array_push($tmparr[0],array('ItemID'=>'0000','ItemName'=>'碳酸饮料','ItemRate'=>'100%'));    array_push($tmparr[0],array('ItemID'=>'0001','ItemName'=>'果汁饮料','ItemRate'=>'100%'));    $tmparr[] = array('ClsID'=>'02','ClsName'=>'食品类');    $tmp[]=array('ItemID'=>'0101','ItemName'=>'生鲜','ItemRate'=>'3%');    $tmp[]=array('ItemID'=>'0102','ItemName'=>'熟食','ItemRate'=>'3%');    array_push($tmparr[1],$tmp);        $json_str=json_encode($tmparr);    //print_r($tmparr);    echo $json_str;?>


回复讨论(解决方案)

同样的问题在架构嵌套数组的时候没有办法给各个节点增加键值。

你的这个不可以吗?
你想得到什么?

出来的是这个效果
-------------------------

Array
(
    [0] => Array
        (
            [ClsID] => 01
            [ClsName] => 饮料类
            [0] => Array
                (
                    [ItemID] => 0000
                    [ItemName] => 碳酸饮料
                    [ItemRate] => 100%
                )

            [1] => Array
                (
                    [ItemID] => 0001
                    [ItemName] => 果汁饮料
                    [ItemRate] => 100%
                )

        )

    [1] => Array
        (
            [ClsID] => 02
            [ClsName] => 食品类
            [0] => Array
                (
                    [0] => Array
                        (
                            [ItemID] => 0101
                            [ItemName] => 生鲜
                            [ItemRate] => 3%
                        )

                    [1] => Array
                        (
                            [ItemID] => 0102
                            [ItemName] => 熟食
                            [ItemRate] => 3%
                        )

                )

        )

)

对,你的代码出来的就是这个
你想要什么样的?

想实现是这个嵌套数组,特别节点需要一个键值
---------------------------------------------

[clslst]Array
(
    [drink] => Array
        (
            [ClsID] => 01
            [ClsName] => 饮料类
            [sodas] => Array
                (
                    [ItemID] => 0000
                    [ItemName] => 碳酸饮料
                    [ItemRate] => 100%
                )

            [juice] => Array
                (
                    [ItemID] => 0001
                    [ItemName] => 果汁饮料
                    [ItemRate] => 100%
                )

        )

    [food] => Array
        (
            [ClsID] => 02
            [ClsName] => 食品类
            [fresh] => Array
                (
                    [ItemID] => 0101
                    [ItemName] => 生鲜
                    [ItemRate] => 3%
                )

            [cooked] => Array
                (
                    [ItemID] => 0102
                    [ItemName] => 熟食
                    [ItemRate] => 3%
                )

        )

)

求教xuzuning版主指点了,PHP学习得不是很熟悉

你的代码并没有给出键名,我何以知道该是什么?

<?php    /**/    $tmparr[] = array('ClsID'=>'01','ClsName'=>'饮料类');  // [drink]     array_push($tmparr[0],array('ItemID'=>'0000','ItemName'=>'碳酸饮料','ItemRate'=>'100%'));  //[sodas]    array_push($tmparr[0],array('ItemID'=>'0001','ItemName'=>'果汁饮料','ItemRate'=>'100%'));  //[juice]     $tmparr[] = array('ClsID'=>'02','ClsName'=>'食品类');  //[food]    $tmp[]=array('ItemID'=>'0101','ItemName'=>'生鲜','ItemRate'=>'3%');  //[fresh]    $tmp[]=array('ItemID'=>'0102','ItemName'=>'熟食','ItemRate'=>'3%');  //[cooked]    array_push($tmparr[1],$tmp);         $json_str=json_encode($tmparr);    //print_r($tmparr);    echo $json_str;?>

键名在后面有注释

那你直接写上不就得了

    $tmparr['drink'] = array('ClsID'=>'01','ClsName'=>'饮料类');  // [drink]     $tmparr['drink']['sodas'] = array('ItemID'=>'0000','ItemName'=>'碳酸饮料','ItemRate'=>'100%');  //[sodas]    $tmparr['drink']['juice'] = array('ItemID'=>'0001','ItemName'=>'果汁饮料','ItemRate'=>'100%');  //[juice]      $tmparr['food'] = array('ClsID'=>'02','ClsName'=>'食品类');  //[food]    $tmparr['food']['fresh'] = array('ItemID'=>'0101','ItemName'=>'生鲜','ItemRate'=>'3%');  //[fresh]    $tmparr['food']['cooked'] = array('ItemID'=>'0102','ItemName'=>'熟食','ItemRate'=>'3%');  //[cooked]

谢谢,搞半天搞复杂了。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:php数组拆分、组合Next article:php写xml文件问题