2

I need to decode a json file and display it in the page.. My code is not working, and i don't know whats wrong. The output is just a blank page.

Here is the json file..

{
   "data": [
      {
         "name": "Jhaimee",
         "uid": 10000
      },
      {
         "name": "Ally",
         "uid": 10000133
      },
      {
         "name": "Macers",
         "uid": 1000056
      },
      {
         "name": "Diego",
         "uid": 100004
      },
      {
         "name": "Killersmile",
         "uid": 1000050
      },
      {
         "name": "Joel",
         "uid": 1000011
      }
   ]
}

I want the output to be..

Jhaimee
Ally
Macers
Diego
Killersmile
Joel

Here is my php.

$data = file_get_contents("https://graph.facebook.com/fql?q=SELECT name,uid FROM user WHERE uid IN  (SELECT  recipients FROM thread WHERE folder_id = 0 ORDER BY message_count DESC) AND uid != me() LIMIT 10&access_token=xxxx");
$data = json_decode($data, true);
echo $data[0]["name"];
4
  • What happens when you run this code? Commented Oct 25, 2014 at 5:38
  • 1
    Step one: Enable error reporting Commented Oct 25, 2014 at 5:42
  • To display the names, you can just do: foreach ($data['data'] as $sub) { echo "$sub\n"; }. Commented Oct 25, 2014 at 5:43
  • Sure about initial var $data contains json string? Commented Oct 25, 2014 at 5:47

1 Answer 1

3

The outermost array key should be data echo $data['data'][0]['name'] foreach($data['data'] as $person){ echo $person['name'] . "<br />\n"; }

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

2 Comments

Is theres a way to display the names without using foreach? Just using echo?
You could use echo print_r($data['data'], true); just to dump the array out. To format it, though, you have to iterate through the array

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.