如何以JSON格式获取多维数组上的特定数据

时间:2018-10-20 12:28:23

标签: php json codeigniter

我有这个JSON格式的多维数组。

{
     "CUSTOMER_ORDER": 
        [
         {
         "customer_number": "51",
                "table_no": "7",
                "menu_name": "Fried Chicken",
                "menu_quatity": "5",
                "menu_price": "200",
                "order_total_price": "1000"
         },
         {
           "customer_number": "51",
           "table_no": "7",
           "menu_name": "Fries",
           "menu_quatity": "5",
           "menu_price": "200",
           "order_total_price": "1000"
         }
        ]
    }

我的问题是我如何获取CUSTOMER_ORDER的索引0上的特定数据?

例如,我想得到一个menu_name的{​​{1}},谢谢。

更新

解码JSON数据后遇到麻烦,我得到的是这样:

Fried Chicken

当我使用这个时:

json_decode() expects parameter 1 to be string, array given

当我尝试此操作时:

$json_data = $this->post('CUSTOMER_ORDER');
$json_decoded = json_decode($json_data);
$customer_nickname = $json_decoded->CUSTOMER_ORDER[0]->customer_nickname;

通过我使用CodeIgniter 3的方式,希望有人帮助我。谢谢。

1 个答案:

答案 0 :(得分:1)

希望对您有帮助。

 $json = '{
         "CUSTOMER_ORDER": 
            [
             {
             "customer_number": "51",
                    "table_no": "7",
                    "menu_name": "Fried Chicken",
                    "menu_quatity": "5",
                    "menu_price": "200",
                    "order_total_price": "1000"
             },
             {
               "customer_number": "51",
               "table_no": "7",
               "menu_name": "Fries",
               "menu_quatity": "5",
               "menu_price": "200",
               "order_total_price": "1000"
             }
            ]
        }';

        $jsonDecodedArray = json_decode($json);
    print_r($jsonDecodedArray->CUSTOMER_ORDER[0]->customer_number);

    // OUTPUT 51