我想將多級 JSON 檔案轉換為 PHP 陣列,但它不起作用,我不知道該怎么做。我想獲取 id 值的數量寬度值。
這是我的 JSON
[
{
"id": 2114,
"stocks": [
{
"quantity": 0,
"minHandlingDays": 1,
"maxHandlingDays": 2
}
],
"sku": "B1005122"
},
{
"id": 2115,
"stocks": [
{
"quantity": 0,
"minHandlingDays": 1,
"maxHandlingDays": 2
}
],
"sku": "B1005123"
},
{
"id": 2139,
"stocks": [
{
"quantity": 0,
"minHandlingDays": 1,
"maxHandlingDays": 2
}
],
"sku": "B1020141"
},
{
"id": 2141,
"stocks": [
{
"quantity": 0,
"minHandlingDays": 1,
"maxHandlingDays": 2
}
],
"sku": "B1020139"
}
]
在這里我最新但提交的嘗試:
$array = json_decode($json, true);
for($i = 0; $i <= count($array); $i ){
$json2 = $array[$i]['stocks'];
$array2 = json_decode(strval($json2));
print_r($array2);
}
uj5u.com熱心網友回復:
你可以使用
$array = json_decode($json, true);
uj5u.com熱心網友回復:
該json_decode函式將整個json字串轉換為一個陣列,甚至是一個多維陣列。因此,使用該功能一次就足夠了。
像這樣試試
$array = json_decode($json, true);
foreach($array as $value){
// with foreach
foreach($value['stocks'] as $stock){
print_r($stock['quantity']);
}
// or
print_r($value['stocks'][0]['quantity']);
}
uj5u.com熱心網友回復:
你必須像這樣使用它:
$array=json_decode($json);
$count=count($array);
for($i=0;$i<$count;$i )
{
print_r($array[$i]->stocks[0]->quantity);
}
因為它被決議為陣列和物件的混合。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/441575.html
