Jump to content

Trouble with arrays, reckon its very simple


mikka23

Recommended Posts

Hi I am trying to pull some information from the themeforest API.  I implemented it fine when the information was under just one array but in this case it seems to be under popular array and then items-last-week array.

 

This is the structure of the API:

http://marketplace.envato.com/api/edge/popular:themeforest.xml

 

Here is my code,  I am trying to get the information for items-last-week.

 

    <?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://marketplace.envato.com/api/edge/popular:themeforest.json');
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$ch_data = curl_exec($ch);
curl_close($ch);

if(!empty($ch_data))
{
$json_data = json_decode($ch_data, true);
$data_count = count($json_data['popular']) -1;
for($i = 0; $i <= $data_count; $i++)
{
	echo '<li><a href="',$json_data['popular'][$i]['url'],'"><img src="',$json_data['popular'][$i]['thumbnail'],'" alt="" height="80px" width="80px"/></a><p><a href="',$json_data['popular'][$i]['url'],'">',$json_data['popular'][$i]['item'],'</a><br>$',$json_data['popular'][$i]['cost'],'</p></li>';
}
}
else 
{
echo 'Sorry, but there was a problem connecting to the API.';
}
?>

 

Any ideas?  Help is greatly appreciated.

 

I though I could just replace

,$json_data['popular'][$i]['url']

with

 ,$json_data['popular'][$i]['items-last-week']['url']

but obviously not

Going to need a var_dump() of the array then you should be able to see it's exact structure. Then you can decide how to loop through it. If var_dump is not deep enough you can create a function with a foreach in it that will loop deeper and deeper if the value is an array. If you need more information on how to do just that let me know. But the quick of it is that the function in the foreach finds an array as the value and then calls it's self to go deeper.

jl5501, your comment is not always true that depends on PHP's setting for that server. If the server has xdebug installed then you will have to modify the ini to say "xdebug.var_display_max_depth=n" or else you will need to use a function in order to loop deep enough. That is the reason that I presented that option.

 

Mikka23, sorry but I can not see that page on my work computer, you will have to post the out put in a code box for me.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.