Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

SChaput

macrumors regular
Original poster
Jul 2, 2008
153
0
United States
Good Day All,

Currently I'm using php to read a webpage which is solely a JSON array. The data is similar to this:
Code:
{
   "data": [
      {
         "name": "name1",
         "id": "4363523"
      },
      {
         "name": "name2",
         "id": "54725362"
      },
      {
         "name": "name3",
         "id": "646262"
      }

     . . .

   ]
}

By using file_get_contents i can retrieve the above information but what is the easiest way to cycle through the entire list? SHould i be able to use some sort of $data['name'][1] or something like that?
Or is my best bet to use some complicated strpos and substr commands to try and isolate what I need?

Any help is greatly appreciated. Thanks!
 

phantax

macrumors member
Feb 2, 2009
72
0
json_decode function should put the data into a multidimensional array which you can then loop through for your keys / vals.
 

Ride9650

macrumors 6502
Jun 29, 2007
352
0
To add on to what phantax said, you'll likely have to navigate to the page using "fopen" or "cUrl" since "file_get_contents" returns the information as a string. After that then use "json_decode"
 

phantax

macrumors member
Feb 2, 2009
72
0
To add on to what phantax said, you'll likely have to navigate to the page using "fopen" or "cUrl" since "file_get_contents" returns the information as a string. After that then use "json_decode"

A lot of shared hosting environments disable fopen for remote URLs, so cUrl is your best bet.
 

SChaput

macrumors regular
Original poster
Jul 2, 2008
153
0
United States
Thanks for all your help everyone. I ended up using curl and it seems to have gotten the data in an array however, I was hoping someone could explain more how to call individual fields of the data, here is what the Curl and the json_decode did for me:
Code:
function get_data($url) 

{ 

  $ch = curl_init();

  $timeout = 5;

  curl_setopt($ch,CURLOPT_URL,$url);

  curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);

  curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);

  $data = curl_exec($ch);

  curl_close($ch);

  return $data;

}

Code:
$returned_content = get_data('url of a page');
$stuff = json_decode($returned_content, true);
print_r($stuff);
Outputs the following:

Code:
Array ( [data] => Array ( [0] => Array ( [name] => name1 [id] => 1251 ) [1] => Array ( [name] => name2 [id] => 423423 ) [2] => Array ( [name] => name3 [id] => 42322323 )

So how would i cycle through each one of these results to pull, just the ID for example?

Thanks again!
 

Ride9650

macrumors 6502
Jun 29, 2007
352
0
Thanks for all your help everyone. I ended up using curl and it seems to have gotten the data in an array however, I was hoping someone could explain more how to call individual fields of the data, here is what the Curl and the json_decode did for me:
Code:
function get_data($url) 

{ 

  $ch = curl_init();

  $timeout = 5;

  curl_setopt($ch,CURLOPT_URL,$url);

  curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);

  curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);

  $data = curl_exec($ch);

  curl_close($ch);

  return $data;

}

Code:
$returned_content = get_data('url of a page');
$stuff = json_decode($returned_content, true);
print_r($stuff);
Outputs the following:

Code:
Array ( [data] => Array ( [0] => Array ( [name] => name1 [id] => 1251 ) [1] => Array ( [name] => name2 [id] => 423423 ) [2] => Array ( [name] => name3 [id] => 42322323 )

So how would i cycle through each one of these results to pull, just the ID for example?

Thanks again!

You use whats called a "loop" which in short, will constantly do something a specified number of times.

I sometimes have a little trouble reading the statements when using print_r on arrays so I apologize ahead of time if this isn't quite right, but it looks like theres just one primary index called "data". Within "data", each index contains another array with your information.

So to access the ID it'd be something like
Code:
$myArray['data'][0]['id']

Look here under "for" and "foreach" if you're not sure how to loop through everything
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.