如何导出多阵列

时间:2019-01-12 04:41:51

标签: php arrays json

我正在尝试获取 stationList ,但是它不起作用。请帮助我。

显示错误:-

  

E_WARNING:类型2-为foreach()提供了无效的参数

$output= '{"apiStatus": 
                 {"message":"SUCCESS","success":true},
           "stationList":
                 {"stationName":"10th.mile","stationId":-1}, 
                 {"stationName":"4 Th Mile Masjid","stationId":-1}
          }';

$json = json_decode($output, true);
$data =   $json['stationList']; 
$json1 = json_decode($data, true);
foreach($json1 as $r){

    echo $r['stationName'].'<br>';
}

1 个答案:

答案 0 :(得分:0)

您的json应该是这样的

$output= '{
"apiStatus": {
        "message": "SUCCESS",
        "success": "true"
    },
    "stationList": [{
        "stationName": "10th.mile",
        "stationId": -1
    },
    {
        "stationName": "4 Th Mile Masjid",
        "stationId": -1
    }]
}';

  /*convert json into array*/
$json_arr = json_decode($output, true); 
/*get  station list*/
$data_station = $json_arr['stationList'];
foreach($data_station as $r){

    echo $r['stationName'].'<br>';
}