0

I have a JSON-encoded array, but in one of the array values is a "$" in the array name. When I will read the value with the follow code below, didn't get I an value.

<?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/>';
    }
}
?>

Below the JSON array from $item

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
        )

Does anyone know how I can read $id?

2 Answers 2

3

The $id item is contained in an ID item. Try:

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

Edit: I am not sure why you have the nested loops. This should be enough:

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/>';
}
Sign up to request clarification or add additional context in comments.

2 Comments

The suggested code uses $item['$id'] instead of $item['ID']['$id'] (i.e. it's inconsistent with "the answer").
Both solutions doens't work. With $id = $item['ID']['$id'], there is no output from $id With one loop doens't nothing work.
1

Use $item['ID']['$id']. If you find yourself using $item[ID], you use undefined constant ID.

Here is code that works:

$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 />';
}

6 Comments

Both doesn't work. No result :( Below a link to the live script: [link]hq.b10-ict.nl/jotihunt/nieuws.php En the source code [link]hq.b10-ict.nl/jotihunt/nieuws.phps
You have nested foreach, which is wrong. becquerel's code is close in that it removed the nested foreach, but it needs to use $id = $item['ID']['$id'];
I'm affraid that i don't understand what you did mean. I have now above code with one forech. But than there no output from $item['datum'],titel and $id. See also the above link to the script aggain.
Almost there. Replace $key1['datum'] with $item['datum']
My bad, i was experiment with variables. Both vars don't give a about
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.