craigward Posted February 19, 2010 Share Posted February 19, 2010 Hi, I am fairly new to PHP and have been messing around trying different things out. One thing I am stuck with is being able to count the elements of a json string so I can create a loop. I can't find anything on the official PHP site and have been looking on Google for ages. Would love it if someone could help me out. Cheers Craig Quote Link to comment Share on other sites More sharing options...
salathe Posted February 19, 2010 Share Posted February 19, 2010 You can convert the JSON string into a PHP array (or object, if you want) then count and/or loop over that. Take a look at the json_decode function. Quote Link to comment Share on other sites More sharing options...
taquitosensei Posted February 19, 2010 Share Posted February 19, 2010 you'll have to decode it. If you have php 5.1 ( I think it might be 5.2 ) you can use the built in function $array=json_decode($arraywithyourjsonconents); foreach($array as $k=>$v) { } //or for($a=0;$a<count($array);$a++) { } if it's before that version of php then you'll have to find a php class. I'm pretty sure there's a pear class available. Quote Link to comment Share on other sites More sharing options...
craigward Posted February 19, 2010 Author Share Posted February 19, 2010 Thanks for the replies. Here is the code I am using. $flickrfeed = json_decode($filecontents); $i = 0; $total = count($flickerfeed); while ($i <= $total) { $farm = $flickrfeed->photos->photo[$i]->farm; $id = $flickrfeed->photos->photo[$i]->id; $server = $flickrfeed->photos->photo[$i]->server; $secret = $flickrfeed->photos->photo[$i]->secret; echo "<img src=\"http://farm$farm.static.flickr.com/$server/$id"._."$secret.jpg\" alt=\"Flickr Image\" />"; $i++; } ?> I have tried to use the foreach loop but havent had luck yet. but I am probably doing something wrong. Quote Link to comment Share on other sites More sharing options...
craigward Posted February 19, 2010 Author Share Posted February 19, 2010 I can't get the for loop to work. $flickrfeed = json_decode($filecontents); for($a=0;$a<count($flickrfeed);$a++) { $farm = $flickrfeed->photos->photo[$a]->farm; $id = $flickrfeed->photos->photo[$a]->id; $server = $flickrfeed->photos->photo[$a]->server; $secret = $flickrfeed->photos->photo[$a]->secret; echo "<img src=\"http://farm$farm.static.flickr.com/$server/$id"._."$secret.jpg\" alt=\"Flickr Image\" />"; } ?> But it does work if I use for($a=0;$a<10;$a++) Quote Link to comment Share on other sites More sharing options...
salathe Posted February 19, 2010 Share Posted February 19, 2010 You'll probably want to count($flickrfeed->photos->photo) Quote Link to comment Share on other sites More sharing options...
craigward Posted February 19, 2010 Author Share Posted February 19, 2010 Cheers salathe Your a star, everything working. Been looking for that answer for ages. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.