我如何在php中将单个数组转换为多维数组?

时间:2015-11-04 05:45:47

标签: php arrays json multidimensional-array

如何将此数组转换为多维数组以在json中显示?

[
    [
         {
         "kitchen_id":"1",
         "thali_id":"1",
         "menu_id":"1",
         "meal_category":"Choti Bhook",
         "thali_name":"Diet",
         "menu_item":"Dal Fry",
         "item_qty":"1"
         },
         {"kitchen_id":"1","thali_id":"1","menu_id":"1","meal_category":"Choti Bhook","thali_name":"Diet","menu_item":"Naan","item_qty":"1"},
         {"kitchen_id":"1","thali_id":"1","menu_id":"1","meal_category":"Choti Bhook","thali_name":"Diet","menu_item":"Roti","item_qty":"1"},
         {"kitchen_id":"1","thali_id":"2","menu_id":"2","meal_category":"Choti Bhook","thali_name":"Maharaja Thali","menu_item":"Dal Fry","item_qty":"2"},
         {"kitchen_id":"1","thali_id":"2","menu_id":"2","meal_category":"Choti Bhook","thali_name":"Maharaja Thali","menu_item":"Naan","item_qty":"2"},
         {"kitchen_id":"1","thali_id":"2","menu_id":"2","meal_category":"Choti Bhook","thali_name":"Maharaja Thali","menu_item":"Roti","item_qty":"1"}
    ]
]

请建议我将代码转换为多维? 我想要这个数据的树视图。 我使用此代码转换为树视图。

$data['Kitchen Id: '.$row1['kitchen_id']]['Thali Id: '.$row1['thali_id']]['Menu Id: '.$row1['menu_id']]['Meal Category: '.$row1['meal_category']]['Thali Name: '.$row1['thali_name']]['Menu Items']=$row1['menu_item'];`

但输出不合适。

提前致谢。

2 个答案:

答案 0 :(得分:1)

使用单引号将数据包装在变量中并将其解码为json

<?php     $a='[[{"kitchen_id":"1","thali_id":"1","menu_id":"1","meal_category":"Choti Bhook","thali_name":"Diet","menu_item":"Dal Fry","item_qty":"1"},{"kitchen_id":"1","thali_id":"1","menu_id":"1","meal_category":"Choti Bhook","thali_name":"Diet","menu_item":"Naan","item_qty":"1"},{"kitchen_id":"1","thali_id":"1","menu_id":"1","meal_category":"Choti Bhook","thali_name":"Diet","menu_item":"Roti","item_qty":"1"},{"kitchen_id":"1","thali_id":"2","menu_id":"2","meal_category":"Choti Bhook","thali_name":"Maharaja Thali","menu_item":"Dal Fry","item_qty":"2"},{"kitchen_id":"1","thali_id":"2","menu_id":"2","meal_category":"Choti Bhook","thali_name":"Maharaja Thali","menu_item":"Naan","item_qty":"2"},{"kitchen_id":"1","thali_id":"2","menu_id":"2","meal_category":"Choti Bhook","thali_name":"Maharaja Thali","menu_item":"Roti","item_qty":"1"}]]';
    $b=json_decode($a);
    echo "<pre>";print_r($b);
    ?>

答案 1 :(得分:0)

$multidimen = json_decode($yourJSON, true);
print_r($multidimen);