Search code examples
phpjsonresponsevar-dump

How can I extract values from the array of JSON data that this var_dump gives


How can I extract title and content from the array of JSON data that gives the following when I 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" }

Using PHP please


Solution

  • This will do it according to your JSON.

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