我有这段代码:
return json_decode($body);
此返回来自一个从谷歌获得一些搜索结果的功能。如果我打印它,例如:
stdClass Object
(
[responseData] => stdClass Object
(
[results] => Array
(
[0] => stdClass Object
(
[GsearchResultClass] => GwebSearch
[unescapedUrl] => http://www.bmw.com/
[url] => http://www.bmw.com/
[visibleUrl] => www.bmw.com
[cacheUrl] => http://www.google.com/search?q=cache:ys7v8m0j3LMJ:www.bmw.com
[title] => BMW automobiles - website of the BMW AG
[titleNoFormatting] => BMW automobiles - website of the BMW AG
[content] => The official BMW AG website: BMW automobiles, services, technologies and all about BMWs sheer driving pleasure.
)
[1] => stdClass Object
(
[GsearchResultClass] => GwebSearch
[unescapedUrl] => http://www.bmwusa.com/
[url] => http://www.bmwusa.com/
[visibleUrl] => www.bmwusa.com
[cacheUrl] => http://www.google.com/search?q=cache:W59Q9fpm9PkJ:www.bmwusa.com
[title] => BMW of North America, LLC
[titleNoFormatting] => BMW of North America, LLC
[content] => BMW ConnectedDrive. Reads Facebook posts and emails. Forecasts the weather. Maps routes. Parks your car. And much more. The All-New 6 Gran Coupe ...
)
[2] => stdClass Object
(
[GsearchResultClass] => GwebSearch
[unescapedUrl] => http://autos.yahoo.com/bmw/
[url] => http://autos.yahoo.com/bmw/
[visibleUrl] => autos.yahoo.com
[cacheUrl] => http://www.google.com/search?q=cache:yjHBrdiQI5UJ:autos.yahoo.com
[title] => New 2011, 2012 BMW Car Models - Yahoo! Autos
[titleNoFormatting] => New 2011, 2012 BMW Car Models - Yahoo! Autos
[content] => Yahoo! Autos - BMW Cars. Research all BMW 2012, 2011 car models. Compare new BMW vehicles; buy used BMWs for sale.
)
[3] => stdClass Object
(
[GsearchResultClass] => GwebSearch
[unescapedUrl] => http://www.motortrend.com/new_cars/01/bmw/
[url] => http://www.motortrend.com/new_cars/01/bmw/
[visibleUrl] => www.motortrend.com
[cacheUrl] => http://www.google.com/search?q=cache:8ur1ZNau24AJ:www.motortrend.com
[title] => New BMW Cars - Find 2012 2013 BMW Car Prices & Reviews ...
[titleNoFormatting] => New BMW Cars - Find 2012 2013 BMW Car Prices & Reviews ...
[content] => Matches 1 - 14 of 14 ... Find new BMW cars and 2012 2013 BMW cars at Motor Trend. Research a new BMW car, find BMW prices, read reviews, or buy a new ...
)
)
[cursor] => stdClass Object
(
[resultCount] => 16,700,000
[pages] => Array
(
[0] => stdClass Object
(
[start] => 0
[label] => 1
)
[1] => stdClass Object
(
[start] => 4
[label] => 2
)
[2] => stdClass Object
(
[start] => 8
[label] => 3
)
[3] => stdClass Object
(
[start] => 12
[label] => 4
)
[4] => stdClass Object
(
[start] => 16
[label] => 5
)
[5] => stdClass Object
(
[start] => 20
[label] => 6
)
[6] => stdClass Object
(
[start] => 24
[label] => 7
)
[7] => stdClass Object
(
[start] => 28
[label] => 8
)
)
[estimatedResultCount] => 16700000
[currentPageIndex] => 0
[moreResultsUrl] => http://www.google.com/search?oe=utf8&ie=utf8&source=uds&start=0&hl=en&q=bmw+cars
[searchResultTime] => 0.15
)
)
[responseDetails] =>
[responseStatus] => 200
)
现在,我尝试使用函数get_object_vars()
,但会输出相同的输出。我想在数组中加入值,但只有[ur] => ;
[title] =>
和[content]=> ;
我该如何实现?
答案 0 :(得分:0)
混合json_decode(字符串$ json [,bool $ assoc = false [,int $ depth = 512 [,int $ options = 0]]])
只需将第二个参数设置为true
;)
答案 1 :(得分:0)
答案 2 :(得分:0)
假设上面的数据保存在名为$returnedData
的变量中:
如果您使用return json_decode($body);
:
$results = array();
foreach ($returnedData->responseData->results as $result) {
$results[] = array(
'url' => $result->url,
'title' => $result->title,
'content'=>$result->content
);
}
print_r($results);
...如果您使用return json_decode($body,true);
:
$results = array();
foreach ($returnedData['responseData']['results'] as $result) {
$results[] = array(
'url' => $result['url'],
'title' => $result['title'],
'content'=>$result['content']
);
}
print_r($results);