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 Link to comment https://forums.phpfreaks.com/topic/192668-count-items-in-a-json-string/ 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. Link to comment https://forums.phpfreaks.com/topic/192668-count-items-in-a-json-string/#findComment-1014996 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. Link to comment https://forums.phpfreaks.com/topic/192668-count-items-in-a-json-string/#findComment-1014997 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. Link to comment https://forums.phpfreaks.com/topic/192668-count-items-in-a-json-string/#findComment-1015002 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++) Link to comment https://forums.phpfreaks.com/topic/192668-count-items-in-a-json-string/#findComment-1015022 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) Link to comment https://forums.phpfreaks.com/topic/192668-count-items-in-a-json-string/#findComment-1015033 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. Link to comment https://forums.phpfreaks.com/topic/192668-count-items-in-a-json-string/#findComment-1015037 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.