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

如何在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.

  • 写回答

3条回答 默认 最新

  • duanjiuhong5843 2018-02-17 21:58
    关注

    The JSON contains an array of one element, so to access provider_id and nro_chart, get the first element and directly access the properties:

    $j = json_decode($json);
    var_dump($j[0]->provider_id); // /Applications/MAMP/htdocs/trello/up.php:28:int 1
    var_dump($j[0]->nro_chart); // /Applications/MAMP/htdocs/trello/up.php:28:int 1
    

    Demo

    Or, within that same foreach loop:

    foreach ($j as $item) {
        array_push($result,[
            'total_value' => $item->control->total_value,
            'car_id' => $item->control->car_id
        ]);
        if (isset($item->provider_id)) {
            var_dump($item->provider_id);
        }
        if (isset($item->nro_chart)) {
            var_dump($item->nro_chart);
        }
    }
    

    Be sure to check if the property is set for that element with isset() first.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • dongzhuo6137 2018-02-17 22:04
    关注
    json_decode($json, true);
    

    will transform your json string to an associative array. so now you can use : $item['provider_id'].

    评论
  • dongquechan4414 2018-02-18 08:55
    关注

    Here is a way to modify the array and declare the two extra variables with less iterated functions.

    Code: (Demo)

    $array = json_decode($json,true);  // decode as array because that is what you are generating in the result
    
    $provider_id=$array[0]['provider_id'];
    $nro_chart=$array[0]['nro_chart'];
    
    foreach($array as $index=>&$subarray){  // modify $subarray by reference
        $subarray=['total_value'=>$subarray['control']['total_value'],'car_id'=>$subarray['control']['car_id']];  // overwrite each subarray using the first two elements from deeper "control" subarray
    }
    
    echo "provider_id = $provider_id
    ";  // display integer value
    echo "nro_chart = $nro_chart
    ";      // display integer value
    var_export($array);                   // display updated array
    

    Output:

    provider_id = 1
    nro_chart = 1
    array (
      0 => 
      array (
        'total_value' => 21.5,
        'car_id' => 421118,
      ),
      1 => 
      array (
        'total_value' => 25,
        'car_id' => 421119,
      ),
      2 => 
      array (
        'total_value' => 18,
        'car_id' => 421519,
      ),
    )
    
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 寻涂色内存脚本作者有项目有市场有资源.却技术
  • ¥15 蓝桥杯c51单片机问题
  • ¥15 ajax跨域问题请求修改代码
  • ¥15 python matplotlib
  • ¥15 短信测压+语音,有偿,必须用Python
  • ¥20 COCOS2DX的protobuf协议注册函数问题
  • ¥15 (标签-Pytorch|关键词-Stream)
  • ¥15 求深圳2019年开放数据应用创新大赛的营运车辆数据!
  • ¥15 软件UI界面绘制折线图
  • ¥20 用c语言写一个团队考勤系统