Need Help Printing an Array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ulyssesr
Forum Newbie
Posts: 4
Joined: Tue Oct 20, 2009 9:35 am

Need Help Printing an Array

Post by ulyssesr »

Hello. I'm new to this forum. I thought I give this forum a try. I've been fighting with printing an array for a couple of hours now. I need to be able to display it on the screen, but I can't seem to figure it out. The value of the $json array is:

[{"position":null,"name":"#jobs","query":"#jobs","id":1431729,"created_at":"Thu Aug 27 19:30:55 +0000 2009"},{"position":null,"name":"ubuntu","query":"ubuntu","id":1449458,"created_at":"Fri Aug 28 21:50:21 +0000 2009"},{"position":null,"name":"linux","query":"linux","id":1650471,"created_at":"Sat Sep 12 00:20:18 +0000 2009"},{"position":null,"name":"#wordpress","query":"#wordpress","id":1912615,"created_at":"Tue Sep 29 11:18:41 +0000 2009"}]

This is my code: (I've updated the code. It's an API call to Twitter)

Code: Select all

 
$contents = @file_get_contents("http://twitter.com/saved_searches.json"); 
$json = json_decode($contents);
foreach ($json as $saved_search) {
  echo $saved_search->name;
}
 
What am I doing wrong?
Last edited by ulyssesr on Wed Oct 21, 2009 6:01 am, edited 1 time in total.
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: Need Help Printing an Array

Post by Mark Baker »

Works for me.

What are you seeing when you echo $saved_search->name
ulyssesr
Forum Newbie
Posts: 4
Joined: Tue Oct 20, 2009 9:35 am

Re: Need Help Printing an Array

Post by ulyssesr »

Mark Baker wrote:Works for me.

What are you seeing when you echo $saved_search->name
Actually, I'm not even getting past that point. I'm getting an foreach() error. It says:

"Invalid argument supplied for foreach()"

It's baffling. Something is wrong with the foreach argument, but I can't figure out what or why.
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: Need Help Printing an Array

Post by Mark Baker »

What version of PHP are you running?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Need Help Printing an Array

Post by John Cartwright »

Try treating the decoded json as an associative array

Code: Select all

$json = json_decode($contents, true);
foreach ($json as $saved_search) {
  echo $saved_search['name'];
}
ulyssesr
Forum Newbie
Posts: 4
Joined: Tue Oct 20, 2009 9:35 am

Re: Need Help Printing an Array

Post by ulyssesr »

Mark Baker wrote:What version of PHP are you running?
My host is Dreamhost. I believe it's running default which according to their site is: PHP 5.2.6.
ulyssesr
Forum Newbie
Posts: 4
Joined: Tue Oct 20, 2009 9:35 am

Re: Need Help Printing an Array

Post by ulyssesr »

John Cartwright wrote:Try treating the decoded json as an associative array
Thanks, John. I tried the code you suggested. Still not working. I'm getting a "Invalid argument supplied for foreach()" error. I've updated my original code. It's actually an API call to Twitter. See original code above. In the meantime, I'll do some more research on associative arrays.
Post Reply