1

I have done it a thousand times before but for some reason I cant access array entries using their index/key. The only thing I am doing different is reading the json from a file and then using json_decode to populate this particular array of objects. When I use a foreach loop, I get the $post and the $key, but when I use the key to access the same value in the original array using $posts[$key], it returns nothing. I need to unset some specific entries and passing via reference hasnt helped either. Below is the code:

    $contents = fread($fh, filesize($filepath));
    fclose( $fh );
    $posts = (array)json_decode($contents);

    foreach( $posts as $key => &$post ){
        $post_time = strtotime($post->post_date);
        $now = strtotime('now');
        if( ($now - $post_time) > 86400 ){
            unset($posts[$key]); 
        }
    }  
5
  • What does print_r($posts) return? Commented Apr 28, 2011 at 8:04
  • Check the structure with print_r($posts) and try json_decode($contents, true). Commented Apr 28, 2011 at 8:04
  • `                                        ` Invisible JSON! Commented Apr 28, 2011 at 8:04
  • i removed several print_r($posts) before putting the code here...all is fine...like i mentioned, in the foreach, I get the $key and the $post, only when I try to use the $key with $posts, for unsetting in this case, do i get nothing. Commented Apr 28, 2011 at 8:06
  • json_decode($contents, true) did the trick...i should have checked the manual...typecasting it into array seemed to have worked for me but apparently it has its issues. thanks a lot mate :) Commented Apr 28, 2011 at 8:11

3 Answers 3

8

change

$posts = (array)json_decode($contents);

to

$posts = json_decode($contents, true); - it will return array you need.

https://www.php.net/manual/en/function.json-decode.php

also you can change $now = strtotime('now'); to $now = time(); and move it out of cycle - it's much faster :)

Tnx @binaryLV for hints :)

Sign up to request clarification or add additional context in comments.

3 Comments

Maybe try to explain why he needs to change it that way
I would suggest to use time() rather than date()... Or even better - $_SERVER['REQUEST_TIME']
$posts = json_decode($contents, true); worked. thanks for the prompt reply and the suggestion :)
0

If I remember right, json_decode doesn't return an array by default, but an object. You have to explicitly request that it returns an array instead if you want to foreach() over it.

Comments

0

Yeah, json_decode doesn't return an array by default because the second parameter to the function that allows the return value to be an associative array is false by default.

$assoc = false

This means if you call the json_decode function with only one argument as below

json_decode($myjson);

...you will get an object. But defining a second argument: a boolean value, it will decide whether it will be an associate array or just an object as below:

json_decode($myjson, true);

This will return an associative array with the keys as the object keys and the values as the object entries/values.

Comments

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.