鍍金池/ 問(wèn)答/PHP/ php傳值給JavaScript數(shù)組問(wèn)題

php傳值給JavaScript數(shù)組問(wèn)題

請(qǐng)問(wèn)高人,前端JavaScript數(shù)組格式為:var menu=[{id:"11",text:"菜單1",children:[{id:"111",text:"子菜單1"},{id:"112",text:"子菜單2"}]},后臺(tái)使用php傳值,如何實(shí)現(xiàn)?

回答
編輯回答
薔薇花

json_encode轉(zhuǎn)換成json字符串

2018年7月23日 03:03
編輯回答
選擇

前后端傳值用json格式傳輸
原數(shù)組格式應(yīng)該是

array (
  0 => 
  array (
    'id' => '11',
    'text' => '菜單1',
    'children' => 
    array (
      0 => 
      array (
        'id' => '111',
        'text' => '子菜單1',
      ),
      1 => 
      array (
        'id' => '112',
        'text' => '子菜單2',
      ),
    ),
  ),
)

用json_encode格式化一下返回即可
返回的數(shù)據(jù)應(yīng)該是

[{
    "id": "11",
    "text": "菜單1",
    "children": [{
        "id": "111",
        "text": "子菜單1"
    }, {
        "id": "112",
        "text": "子菜單2"
    }]
}]
2017年6月17日 04:50