Home > Backend Development > PHP Tutorial > 使用PHP架构嵌套数组

使用PHP架构嵌套数组

WBOY
Release: 2016-06-13 11:20:36
Original
1028 people have browsed it

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


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

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


------解决方案--------------------
那你直接写上不就得了
    $tmparr['drink'] = array('ClsID'=>'01','ClsName'=>'饮料类');  // [drink] <br />    $tmparr['drink']['sodas'] = array('ItemID'=>'0000','ItemName'=>'碳酸饮料','ItemRate'=>'100%');  //[sodas]<br />    $tmparr['drink']['juice'] = array('ItemID'=>'0001','ItemName'=>'果汁饮料','ItemRate'=>'100%');  //[juice]<br />  <br />    $tmparr['food'] = array('ClsID'=>'02','ClsName'=>'食品类');  //[food]<br />    $tmparr['food']['fresh'] = array('ItemID'=>'0101','ItemName'=>'生鲜','ItemRate'=>'3%');  //[fresh]<br />    $tmparr['food']['cooked'] = array('ItemID'=>'0102','ItemName'=>'熟食','ItemRate'=>'3%');  //[cooked]<br />
Copy after login

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template