-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
        }
    ]
}
2
  • PHP, I've edited the question to add that. Thanks. Commented Aug 16, 2011 at 22:29
  • 4
    Did you try "json decode php" in Google ? Commented Aug 16, 2011 at 22:30

4 Answers 4

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);
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

0

You might want to read up on json_decode

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.