无法读取数组名称中带有“$”的 JSON 数组值


Can't read JSON array value with a "$" in the array name,

我有一个 JSON 编码的数组,但在其中一个数组值中,数组名称中有一个"$"。当我使用以下代码读取值时,没有得到一个值。

<?php
error_reporting("E_ERROR");
date_default_timezone_set("Europe/Amsterdam"); 
$json = file_get_contents('http://jotihunt.net/api/1.0/nieuws');
$json = json_decode($json, true);
foreach ($json as $key1 => $item) {
    foreach ($item as $key2 => $value) {
        $id = $item['$id'];
         echo gmdate("d-m-Y H:i", strtotime('+2 hours', $value['datum'])) . '&nbsp;' . $value['titel'] . ' met ID: '.$id.'<br/>';
    }
}
?>

$item 中的 JSON 数组下方

Array
(
    [0] => Array
        (
            [ID] => Array
                (
                    [$id] => 52532555a08789e17900000d /* Can't read this with $item[$id] because the "$" before "id" */
                )
            [titel] => API 1.0    /* $value['titel'] */
            [datum] => 1381180320 /* $value['datum'] */
        )
    [1] => Array
        (
            [ID] => Array
                (
                    [$id] => 524b16eaa08789806a000010
                )
            [titel] => Inschrijving gesloten
            [datum] => 1380652260
        )

有谁知道我如何阅读$id

$id项包含在 ID 项中。尝试:

$id = $item['ID']['$id'];

编辑:我不确定为什么你有嵌套循环。这应该足够了:

foreach ($json as $key1 => $item) {
 $id = $item['ID']['$id'];
 echo gmdate("d-m-Y H:i", strtotime('+2 hours', $item['datum'])) . '&nbsp;' . $item['titel'] . ' met ID: '.$id.'<br/>';
}

使用 $item['ID']['$id'] .如果你发现自己使用 $item[ID] ,则使用未定义的常量ID

以下是有效的代码:

$json = file_get_contents('http://jotihunt.net/api/1.0/nieuws');
$json = json_decode($json, true);
foreach ($json['data'] as $key1 => $item) {
  $id = $item['ID']['$id'];
  echo gmdate("d-m-Y H:i", strtotime('+2 hours', $item['datum'])) . '&nbsp;' . $item['titel'] . ' met ID: '.$id.  '<br />';
}