-1

The json data object below is what's returned from a custom Google search API request. I need to extract each of the "url" elements and place them into an array (using PHP).

myArray = {url1, url2, url3, etc...}

How?

data =  '{
"responseData": 
{
    "results":
    [
        {
            //etc
        }
    ]
}
RegEdit
  • 2,134
  • 10
  • 37
  • 63

4 Answers4

2

Am I right that you have JSON string? Use json_decode to decode it. After that you can use

 array_map(function($x){
     return $x->url;
 },$var->responceData->results);

(Requires PHP 5.3 for anonymous function, you can use no anonymous ones if use PHP5.2 or older)

For later versions:

function smth($x){
    return $x->url;
}
array_map('smth',$var->responceData->results);
RiaD
  • 46,822
  • 11
  • 79
  • 123
0

You can use json_decode to get an array corresponding to your JSON and then analyze it like you would do for a normal array.

Cydonia7
  • 3,744
  • 2
  • 23
  • 32
0

Try using:

$myObject=json_decode($myJSONstring);

Here's the reference.

Then do:

$urlArray=array();
foreach($myObject->responseData->results as $myResult) {
    foreach($myResult as $myAttribute => $myValue) {
        $urlArray[] = $myValue;
    }
}

$urlArray will be what you're looking for.

Jonathan M
  • 17,145
  • 9
  • 58
  • 91
0

You might want to read up on json_decode

Rem.co
  • 3,813
  • 3
  • 29
  • 37