Author Topic: Trouble with arrays, reckon its very simple  (Read 231 times)

0 Members and 1 Guest are viewing this topic.

Offline mikka23Topic starter

  • Irregular
  • Posts: 9
    • View Profile
Trouble with arrays, reckon its very simple
« on: February 09, 2010, 10:08:22 AM »
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($chCURLOPT_URL'http://marketplace.envato.com/api/edge/popular:themeforest.json');
curl_setopt($chCURLOPT_CONNECTTIMEOUT5);
curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
$ch_data curl_exec($ch);
curl_close($ch);
 
if(!empty(
$ch_data))
{
	
$json_data json_decode($ch_datatrue);
	
$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
Code: [Select]
,$json_data['popular'][$i]['url'] with
Code: [Select]
,$json_data['popular'][$i]['items-last-week']['url'] but obviously not
« Last Edit: February 09, 2010, 10:19:26 AM by mikka23 »

Offline WolfRage

  • Devotee
  • Posts: 651
  • Gender: Male
    • View Profile
    • Feral Bytes
Re: Trouble with arrays, reckon its very simple
« Reply #1 on: February 09, 2010, 10:33:18 AM »
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.
-- * -- * -- * -- * -- * -- * -- * -- * -- * -- * -- * -- * -- * -- * -- * -- * -- * -- * -- * -- * -- * -- * --
Please be forewarned; rather than giving you exactly what you want I prefer to teach you how to get what you want. Knowledge is power, so take the time to learn PHP and you will be able to wield it's power.
If I just gave you the code to solve your problem then you will be back again tomorrow asking for more of the same code. So please take the time to learn. Thanks.

Offline jl5501

  • Devotee
  • Posts: 728
  • Gender: Male
  • PHP Freelancer
    • View Profile
Re: Trouble with arrays, reckon its very simple
« Reply #2 on: February 09, 2010, 10:41:06 AM »
both var_dump() and print_r() will recurse to the lowest level of an array so you get to see all levels
Secure forms without Captcha

Offline mikka23Topic starter

  • Irregular
  • Posts: 9
    • View Profile
Re: Trouble with arrays, reckon its very simple
« Reply #3 on: February 09, 2010, 10:42:38 AM »
Here is a var dump of  json_decode($ch_data, true).

http://freecss.info/colorful/wp-content/themes/FreeCSS/popular.php

I have done that already.  My problem is I dont know how to select the info i want even if I know the structure.
« Last Edit: February 09, 2010, 10:43:32 AM by mikka23 »

Offline WolfRage

  • Devotee
  • Posts: 651
  • Gender: Male
    • View Profile
    • Feral Bytes
Re: Trouble with arrays, reckon its very simple
« Reply #4 on: February 10, 2010, 05:02:30 AM »
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.
-- * -- * -- * -- * -- * -- * -- * -- * -- * -- * -- * -- * -- * -- * -- * -- * -- * -- * -- * -- * -- * -- * --
Please be forewarned; rather than giving you exactly what you want I prefer to teach you how to get what you want. Knowledge is power, so take the time to learn PHP and you will be able to wield it's power.
If I just gave you the code to solve your problem then you will be back again tomorrow asking for more of the same code. So please take the time to learn. Thanks.