如何从此var_dump提供的JSON数据数组中提取值

时间:2017-01-31 08:10:51

标签: php json response var-dump

如何从JSON数据数组中提取titlecontent,以便在var_dump(WpApi::posts());

时提供以下内容
array(3) { ["results"]=> array(1) 
{ [0]=> array(23) 
{ ["id"]=> int(8) 
["date"]=> string(19) "2017-01-31T07:08:21" 
["date_gmt"]=> string(19) "2017-01-31T07:08:21" 
["guid"]=> array(1) { ["rendered"]=> string(34) "http://idybrand.com/wordpress/?p=8" }
["modified"]=> string(19) "2017-01-31T07:08:21" 
["modified_gmt"]=> string(19) "2017-01-31T07:08:21" 
["slug"]=> string(34) "february-is-just-around-the-corner" 
["type"]=> string(4) "post" 
["link"]=> string(76) "http://idybrand.com/wordpress/2017/01/31/february-is-just-around-the-corner/" 
["title"]=> array(1) { ["rendered"]=> string(34) "February is just around the corner" } 
["content"]=> array(2) { ["rendered"]=> string(39) "
Appreciate this wonderful month
" ["protected"]=> bool(false) } 
["excerpt"]=> array(2) { ["rendered"]=> string(39) "
Appreciate this wonderful month
" ["protected"]=> bool(false) } 
["author"]=> int(1) 
["featured_media"]=> int(0) 
["comment_status"]=> string(4) "open" 
["ping_status"]=> string(4) "open" 
["sticky"]=> bool(false) 
["template"]=> string(0) "" 
["format"]=> string(8) "standard" 
["meta"]=> array(0) { } 
["categories"]=> array(1) { [0]=> int(1) } 
["tags"]=> array(0) { } 
["_links"]=> array(9) { ["self"]=> array(1) { [0]=> array(1) { ["href"]=> string(51) "http://idybrand.com/wordpress/wp-json/wp/v2/posts/8" } } ["collection"]=> array(1) { [0]=> array(1) { ["href"]=> string(49) "http://idybrand.com/wordpress/wp-json/wp/v2/posts" } } ["about"]=> array(1) { [0]=> array(1) { ["href"]=> string(54) "http://idybrand.com/wordpress/wp-json/wp/v2/types/post" } } ["author"]=> array(1) { [0]=> array(2) { ["embeddable"]=> bool(true) ["href"]=> string(51) "http://idybrand.com/wordpress/wp-json/wp/v2/users/1" } } ["replies"]=> array(1) { [0]=> array(2) { ["embeddable"]=> bool(true) ["href"]=> string(59) "http://idybrand.com/wordpress/wp-json/wp/v2/comments?post=8" } } ["version-history"]=> array(1) { [0]=> array(1) { ["href"]=> string(61) "http://idybrand.com/wordpress/wp-json/wp/v2/posts/8/revisions" } } ["wp:attachment"]=> array(1) { [0]=> array(1) { ["href"]=> string(58) "http://idybrand.com/wordpress/wp-json/wp/v2/media?parent=8" } } ["wp:term"]=> array(2) { [0]=> array(3) { ["taxonomy"]=> string(8) "category" ["embeddable"]=> bool(true) ["href"]=> string(61) "http://idybrand.com/wordpress/wp-json/wp/v2/categories?post=8" } [1]=> array(3) { ["taxonomy"]=> string(8) "post_tag" ["embeddable"]=> bool(true) ["href"]=> string(55) "http://idybrand.com/wordpress/wp-json/wp/v2/tags?post=8" } } ["curies"]=> array(1) { [0]=> array(3) { ["name"]=> string(2) "wp" ["href"]=> string(23) "https://api.w.org/{rel}" ["templated"]=> bool(true) } } } } } 
["total"]=> string(1) "1" 
["pages"]=> string(1) "1" }

请使用PHP

2 个答案:

答案 0 :(得分:4)

这将根据您的JSON进行。

echo ['results'][0]['title']['rendered']; //Title
echo ['results'][0]['content']['rendered']; //Content

答案 1 :(得分:1)

你有一个多维数组,所以你要从中获取数据。

$arr = WpApi::posts();
$arr['results'][0]['content']['rendered'] //this is your content string
$arr['results'][0]['title']['rendered'] // this is your title string