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
}
]
}
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);
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.
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.
You might want to read up on json_decode