dongzi1209
2018-02-17 21:53
浏览 1.1k
已采纳

如何在php中读取多级json的值?

I have the following code to read a Json and store some values into an array:

<?php
$json = '[{
        "provider_id":1,
        "nro_chart":1,
        "control": {
            "color": "blue",
            "total_value": 21.5,
            "car_id": 421118
        }
    },
    {
        "control": {
            "color": "green",
            "total_value": 25,
            "car_id": 421119
        }
    },
    {
        "control": {
            "color": "red",
            "total_value": 18,
            "car_id": 421519
        }
    }
]';

$j = json_decode($data);
$result = [];
foreach ($j as $item) {
    array_push($result,[
        'total_value' => $item->control->total_value,
        'car_id' => $item->control->car_id
    ]);
}

Now I need to get the two values that are outside of "control" like "provider_id" and "nro_chart" inside a variable.

图片转代码服务由CSDN问答提供 功能建议

我有以下代码来读取Json并将一些值存储到数组中:</ p> \ n

 &lt;?php 
 $ json ='[{
“”provider_id“:1,
”nro_chart“:1,
”control“:{
”color“:”blue  “,
”total_value“:21.5,
”n“car_id”:421118 
} 
},
 {
“control”:{
“color”:“green”,
“total_value”:  25,
“car_id”:421119 
} 
},
 {
“control”:{
“color”:“red”,
“total_value”:18,
“car_id”:  421519 
} 
} 
]'; 
 
 $ j = json_decode($ data); 
 $ result = []; 
foreach($ j as $ item){
 array_push($ result,  [
'total_value'=&gt; $ item-&gt; control-&gt; total_value,
'car_id'=&gt; $ item-&gt; control-&gt; car_id 
]); 
} 
 </  code> </ pre> 
 
 

现在我需要在变量内部获取“control_id”和“nro_chart”之外的两个“控制”值。</ p > </ div>

3条回答 默认 最新

相关推荐 更多相似问题