PHP通过JSON循环以创建新数组

时间:2019-06-26 07:27:15

标签: php arrays json

我不确定为什么我会遇到这么麻烦,但是我有一些解码后的JSON,我想遍历它来使用一些数据构建一个较小的数组。

下面是我的JSON $jsonData

{
  "resultsPage": {
    "results": {
      "event": [
        {
          "id":11129128,
          "type":"Concert",
          "uri":"http://www.songkick.com/concerts/11129128-wild-flag-at-fillmore?utm_source=PARTNER_ID&utm_medium=partner",
          "displayName":"Wild Flag at The Fillmore (April 18, 2012)",
          "start": {
            "time":"20:00:00",
            "date":"2012-04-18",
            "datetime":"2012-04-18T20:00:00-0800"
          },
       location": {
            "city":"Chicago, IL, US",
            "lng":-134.903409,
            "lat":37.7842398
          },
          "venue": {
            "id":6239,
            "displayName":"The Fillmore",
            "uri":"http://www.songkick.com/venues/6239-fillmore?utm_source=PARTNER_ID&utm_medium=partner",
            "lng":-122.4332937,
            "lat":37.7842398,
            "metroArea": {
              "id":26330,
              "uri":"http://www.songkick.com/metro_areas/26330-us-sf-bay-area?utm_source=PARTNER_ID&utm_medium=partner",
              "displayName":"SF Bay Area",
              "country": { "displayName":"US" },
              "state": { "displayName":"CA" }
            }
          },
          "status":"ok",
          "popularity":0.012763
        },
        {
          "id":7923094,
          "type":"Concert",
          "uri":"http://www.songkick.com/concerts/11129128-wild-flag-at-fillmore?utm_source=PARTNER_ID&utm_medium=partner",
          "displayName":"Wild Flag at The Fillmore (April 18, 2012)",
          "start": {
            "time":"20:00:00",
            "date":"2012-04-18",
            "datetime":"2012-04-18T20:00:00-0800"
          },
       location": {
            "city":"New York, NY, US",
            "lng":63.902374,
            "lat":49.7842328
          },
          "venue": {
            "id":6239,
            "displayName":"The Fillmore",
            "uri":"http://www.songkick.com/venues/6239-fillmore?utm_source=PARTNER_ID&utm_medium=partner",
            "lng":-122.4332937,
            "lat":37.7842398,
            "metroArea": {
              "id":26330,
              "uri":"http://www.songkick.com/metro_areas/26330-us-sf-bay-area?utm_source=PARTNER_ID&utm_medium=partner",
              "displayName":"SF Bay Area",
              "country": { "displayName":"US" },
              "state": { "displayName":"CA" }
            }
          },
          "status":"ok",
          "popularity":0.012763
        },

        {
          "id":89763146,
          "type":"Concert",
          "uri":"http://www.songkick.com/concerts/11129128-wild-flag-at-fillmore?utm_source=PARTNER_ID&utm_medium=partner",
          "displayName":"Wild Flag at The Fillmore (April 18, 2012)",
          "start": {
            "time":"20:00:00",
            "date":"2012-04-18",
            "datetime":"2012-04-18T20:00:00-0800"
          },
       location": {
            "city":"Miami, FL, US",
            "lng":42.1238243,
            "lat":50.7289731
          },
          "venue": {
            "id":6239,
            "displayName":"The Fillmore",
            "uri":"http://www.songkick.com/venues/6239-fillmore?utm_source=PARTNER_ID&utm_medium=partner",
            "lng":-122.4332937,
            "lat":37.7842398,
            "metroArea": {
              "id":26330,
              "uri":"http://www.songkick.com/metro_areas/26330-us-sf-bay-area?utm_source=PARTNER_ID&utm_medium=partner",
              "displayName":"SF Bay Area",
              "country": { "displayName":"US" },
              "state": { "displayName":"CA" }
            }
          },
          "status":"ok",
          "popularity":0.012763
        }
      ]
    },
    "totalEntries":24,
    "perPage":50,
    "page":1,
    "status":"ok"
  }
}

下面是我第一次尝试遍历JSON变量并将所需的数据解析到新数组中。我不确定是否应该使用push或是否有更有效的方法将我想要的数据解析为新数组:

$newArray = array();

foreach($jsonData['resultsPage']['results']['event'] as $val){

$newArray .= "{'id' => $val['id'], 'long' => $val['location']['lng'], 'lat' => $val['location']['lat']}"

}

3 个答案:

答案 0 :(得分:3)

尝试一下:

$jsondata_array = json_decode($jsonData, true);

$new_required_arr = $jsondata_array['resultsPage']['results']['event'];

foreach($new_required_arr as $key => $val)
{
  //Your logic to create new array with required key value pair.
}

答案 1 :(得分:1)

提供的JSON格式错误,索引location开头缺少双引号。

要解析JSON,您可以将JSON转换为数组并遍历数组以应用逻辑或构建新数组,可以将json_decodetrue一起使用参数。

$arr = json_decode($json, true);
foreach($arr as $k => $v){
  //Your logic
}

答案 2 :(得分:1)

循环的问题是,当元素不存在时,您会收到错误消息,因为您尝试访问数组中不存在的元素。

foreach($jsonData as $val) {
}

然后,您可以使用自己的价值观进行洞察,您应该使用isset进行检查。或者,您可以在循环周围执行if语句来防止该问题的发生。

下一个问题是您将$newArray定义为数组。但是.是要连接一个字符串。因此,您应该这样定义初始变量:

$newArray = '';

最后一件事是我在您的代码中看不到它,但是要解析json字符串,您必须首先使用json_decode。得到一个对象。如果需要数组,则必须将第二个参数设置为true

$arr = json_decode($myjson, true);