1
foreach ($likes as $like) {
    // Extract the pieces of info we need from the requests above
    $id = idx($like, 'id');
    $item = idx($like, 'name');

    fwrite($fileout,json_encode($like));
    fwrite($fileout,PHP_EOL );
}

$json_string = file_get_contents('testson.json');
$get_json_values=json_decode($json_string,true);

foreach ($get_json_values as $getlikes) {  ?>
          <li>
            <a href="https://www.facebook.com/<?php echo $getlikes['id']; ?>" target="_top">
          </li>
        <?php
      } 

When opening the browser, there is a Warning: Invalid argument supplied for foreach(). I don't understand why would my arguments be invalid.

If I add the if, nothing happens, which shows what the actual problem is. But the question is WHAT IS THE PROPER WAY TO DO THIS? I'm pretty sure it's very simple, but i've been struggling with this for more than an hour. My json files has fields like this, so I don't think there would be the problem:

{"category":"Musician\/band","name":"Yann Tiersen (official)","id":"18359161762"}

Please help me, I really got tired with it, and I don't know what to do. So... how can I decode the file into an array?

4
  • 1
    have you tried, json_decode('data.json', true) ? (the true flag is for array output, instead of an object. Commented Mar 11, 2013 at 16:26
  • foreach() works equally well on objects as on arrays - if he's getting an Invalid argument supplied... error, then he's passing something else (probably boolean false). Commented Mar 11, 2013 at 16:31
  • Yes, I have tried the true flag- edited Commented Mar 11, 2013 at 16:36
  • @SamDufel yes, but won't work in the case of $getlikes['id'] Commented Mar 11, 2013 at 16:37

2 Answers 2

2

You need the contents of the testson.json file not just the name!

Receive the contents via PHP:

$json_string = file_get_contents('testson.json');

Make sure there are valid JSON contents in the file by testing it via

var_dump(json_decode($json_string));

And then call

foreach (json_decode($json_string) as $getlikes) {  ?>

Update: When you save the file and access it miliseconds later it might be that the filesystem is too slow and not showing you the correct content.

Add

fclose($fileout);
clearstatcache();

before the file_get_contents(); call!

I recommend to use file_put_contents() and file_read_contents() to get rid of such problems!

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

9 Comments

Thank you so much! However wouldn't it be better to use json_decode($json_string,true), just in case?
@BujancaMihai, it would be more clear. But since PHP tries to convert the types of variables into the necessary ones, it works without here. But be save and use the additional parameter true!
The file 'testson.json' is in the same directory as your PHP script? And do you receive the same error or an other one?
Yes, it is. It actually is encoded by the php, and then decoded again- after some changes
I shall put more code, maybe it would help. It is firstly encoded, then decoded. I am trying to make a facebook app and I need to encode the file if some commands are given, or to decode, if others are given
|
1

json_decode is expecting a string as it's first parameter. You are passing what I'm guessing is a filename. You should load the file first like so...

$json = file_get_contents('testson.json');
$data = json_decode($json);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.