我有这个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"
}
]
}
我的问题是,如何获取索引 0 的特定数据CUSTOMER_ORDER
?
例如,我想得到menu_name
Fried Chicken
,谢谢。
更新
解码 JSON 数据后我遇到问题,我得到的是:
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;
当我尝试这个时:
Undefined index: CUSTOMER_ORDER
$json_decoded = json_decode($json_data,true);
or
$json_decoded = json_decode(json_encode($json_data),true);
$customer_nickname = $json_decoded['CUSTOMER_ORDER'][0]['customer_nickname'];
顺便说一下,我正在使用CodeIgniter 3。我希望有人帮助我。谢谢。
我希望它有所帮助。
$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