循环嵌套json数组PHP

时间:2018-11-18 13:51:15

标签: php arrays json multidimensional-array

邮寄的json字符串示例在这里:

[
  {
    "gc": [
      {
        "id": "1",
        "ti": "title1",
        "oid": 1,
        "mid": "1-2",
        "mc": 2,
        "gct": 1,
        "ma": 1,
        "isu": 0
      }
    ],
    "gcm": [
      {
        "mid": "11",
        "gcid":"1",
        "sid": 58,
        "msg": "msg1 ",
        "sdt": "2018-11-12T13:58:24.627",
        "ma": 20181112135822,
        "isu": 0
      },
      {
        "mid": "12",
        "gcid":"1",
        "sid": 58,
        "msg": "msg2 ",
        "sdt": "2018-11-12T13:58:24.627",
        "ma": 20181112135822,
        "isu": 0
      }
    ],
    "gcms": [
      {
        "id": "111",
        "ma": 1,
        "mid": 58,
        "sdt": "1",
        "isu": 0,
        "msf": 0,
        "gcid": "1",
        "cmid": "11"
      },
      {
        "id": "112",
        "ma": 1,
        "mid": 58,
        "sdt": "1",
        "isu": 0,
        "msf": 0,
        "gcid": "1",
        "cmid": "11"
      },
      {
        "id": "121",
        "ma": 1,
        "mid": 58,
        "sdt": "1",
        "isu": 0,
        "msf": 0,
        "gcid": "1",
        "cmid": "12"
      },
      {
        "id": "122",
        "ma": 1,
        "mid": 58,
        "sdt": "1",
        "isu": 0,
        "msf": 0,
        "gcid": "1",
        "cmid": "12"
      }
    ]
  }
]

我使用以下代码在解码数组中循环,但在数组中仅循环1次:

$json  = json_decode($input, true);
foreach ($json as $value) {
    $this->logger->info("Start update_new_data ->");
    if (isset($value['gc'])) {
        $this->logger->info("gc key exist");
        foreach ($value["gc"] as $jObj) {
        }
    } else {
        if (isset($value['gcm'])) {
            $this->logger->info("gcm key exist");
            foreach ($value["gcm"] as $jObj) {
            }
        } else {
            if (isset($value['gcms'])) {
                $this->logger->info("gcms key exist");
                foreach ($value["gcms"] as $jObj) {
                }
            }
        }
    }
    $this->logger->info("End update_new_data ->");
}

我要在数组中循环,如果key是'gc'循环在'gc'数组中再次循环,并且如果key是'gcm'循环在'gcm'数组中并且如果key是'gcms'循环在'gcms'数组中。我必须检查密钥,因为密钥mybe在json字符串中不存在。 测试许多代码,但不能解决问题。 我该怎么办?

4 个答案:

答案 0 :(得分:1)

@mohammadi这可以为您服务。

$json  = json_decode($input, true);
$json = isset($json[0])?$json[0]:'';
$this->logger->info("Start update_new_data ->");
if(!empty($json)){
foreach ($json as $Key=>$value) {
if(isset($Key)){
if ($Key == "gc") {
    $this->logger->info("gc key exist");
    foreach ($value as $jObj) {
    }
} 
else if ($Key == "gcm") {
    $this->logger->info("gcm key exist");
    foreach ($value as $jObj) {
    }
}
else if ($Key == "gcms") {
     $this->logger->info("gcms key exist");
     foreach ($value as $jObj) {
     }
}
}
$this->logger->info("End update_new_data ->");
}
}

如果您由于整个json日期(数组)不想更改json结构,则可以使用此“ $ json = isset($ json [0])?$ json [0]:”;“行在单个数组中,这样您就可以获取数组的第一个索引。如果您这样更改json(如下所示),则不需要此“ $ json = isset($ json [0])?$ json [0]:”;“”

$input = '{  
    "gc": [
      {
        "id": "1",
        "ti": "title1",
        "oid": 1,
        "mid": "1-2",
        "mc": 2,
        "gct": 1,
        "ma": 1,
        "isu": 0
      }
    ],
    "gcm": [
      {
        "mid": "11",
        "gcid":"1",
        "sid": 58,
        "msg": "msg1 ",
        "sdt": "2018-11-12T13:58:24.627",
        "ma": 20181112135822,
        "isu": 0
      },
      {
        "mid": "12",
        "gcid":"1",
        "sid": 58,
        "msg": "msg2 ",
        "sdt": "2018-11-12T13:58:24.627",
        "ma": 20181112135822,
        "isu": 0
      }
    ],
    "gcms": [
      {
        "id": "111",
        "ma": 1,
        "mid": 58,
        "sdt": "1",
        "isu": 0,
        "msf": 0,
        "gcid": "1",
        "cmid": "11"
      },
      {
        "id": "112",
        "ma": 1,
        "mid": 58,
        "sdt": "1",
        "isu": 0,
        "msf": 0,
        "gcid": "1",
        "cmid": "11"
      },
      {
        "id": "121",
        "ma": 1,
        "mid": 58,
        "sdt": "1",
        "isu": 0,
        "msf": 0,
        "gcid": "1",
        "cmid": "12"
      },
      {
        "id": "122",
        "ma": 1,
        "mid": 58,
        "sdt": "1",
        "isu": 0,
        "msf": 0,
        "gcid": "1",
        "cmid": "12"
      }
    ]
  }';

答案 1 :(得分:0)

尝试使用array_keys进行迭代:

$json  = json_decode($input, true);
$json_keys = array_keys($json);
foreach ($json_keys as $json_key) {
    $item = $json[$json_key];
    foreach ($item as $row) {
        foreach ($row as $subrow) {
            echo isset($subrow['id']) ? $subrow['id'] : 'nope';
        }
    }
}

答案 2 :(得分:0)

根据问题中给出的json,解码后的数组具有单个元素,该元素包围了所有元素。在这种情况下,与其像$json这样循环遍历foreach ($json..,还不如像$json[0]这样遍历foreach ($json[0]...就可以得到密钥,而不必使用array_keys ,只需使用以下键即可运行循环:foreach ($json[0] as $key => $value)...

答案 3 :(得分:0)

我没有任何理由为外部数组使用循环。只需直接访问您想要的零件:

$json  = json_decode($input, true);
$value = $json[0];
$this->logger->info("Start update_new_data ->");
if (isset($value['gc'])) {
    $this->logger->info("gc key exist");
    foreach ($value["gc"] as $jObj) {
    }
} 
if (isset($value['gcm'])) {
    $this->logger->info("gcm key exist");
    foreach ($value["gcm"] as $jObj) {
    }
}
if (isset($value['gcms'])) {
     $this->logger->info("gcms key exist");
     foreach ($value["gcms"] as $jObj) {
     }
}
$this->logger->info("End update_new_data ->");

这样,您可以更清楚地看到json结构(数组内部的数组)。