2017-01-31 1 views
0

Как я могу извлечь title и content из массива данных JSON, который дает следующее, когда я var_dump(WpApi::posts());Как я могу извлечь значения из массива данных JSON, что var_dump дает

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 пожалуйста

+0

Здесь нужно много горизонтальной прокрутки. Не могли бы вы отформатировать исходный код более читаемым способом? – Gustave

+0

@Gustave Отличная идея, но я не совсем уверен, как это сделать. Я попробую ;-) –

ответ

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 
+0

Спасибо. Я думаю, что вы близки, но я получаю «Undefined index: content». Думаю, нам нужно сначала рассмотреть «результаты»? –

+2

Спасибо, но Kitson88 просто избил вас :-) –

Смежные вопросы