2016-11-30 33 views
0

我真的很陌生。从我学习/阅读。要在php中遍历一个对象,您可以使用->并遍历数组["itemInArray"]。这可能听起来很愚蠢,但是如何从下面的这个对象中获取某些东西?说,post_contentPHP如何迭代WP对象

我已经内foreach环环这就是通过多维数组。它看起来像:

foreach ($key as $b=>$test) { 
var_dump($test["post_content"]); 
} 

-

我的目标:

object(WP_Post)#7239 (24) { 
    ["ID"]=> 
    int(2127) 
    ["post_author"]=> 
    string(1) "5" 
    ["post_date"]=> 
    string(19) "2016-04-29 09:45:55" 
    ["post_date_gmt"]=> 
    string(19) "2016-04-29 09:45:55" 
    ["post_content"]=> 
    string(313) "<p class="p1"><span class="s1">Some dummy content</span></p> 
<p class="p1"><em>Support from whateves</em></p>" 
    ["post_title"]=> 
    string(12) "The Title" 
    ["post_excerpt"]=> 
    string(0) "" 
    ["post_status"]=> 
    string(7) "publish" 
    ["comment_status"]=> 
    string(6) "closed" 
    ["ping_status"]=> 
    string(6) "closed" 
    ["post_password"]=> 
    string(0) "" 
    ["post_name"]=> 
    string(12) "the-title" 
    ["to_ping"]=> 
    string(0) "" 
    ["pinged"]=> 
    string(0) "" 
    ["post_modified"]=> 
    string(19) "2016-11-24 14:34:01" 
    ["post_modified_gmt"]=> 
    string(19) "2016-11-24 14:34:01" 
    ["post_content_filtered"]=> 
    string(0) "" 
    ["post_parent"]=> 
    int(0) 
    ["guid"]=> 
    string(66) "http://www.localhost:8080/?post_type=creator&#038;p=2127" 
    ["menu_order"]=> 
    int(0) 
    ["post_type"]=> 
    string(17) "creator" 
    ["post_mime_type"]=> 
    string(0) "" 
    ["comment_count"]=> 
    string(1) "0" 
    ["filter"]=> 
    string(3) "raw" 
} 

回答

1

这应该做的伎俩。

echo $test->post_content;

PHP数组和对象之间的差别是这样的:

阵列:

$array = new Array(); 
$array['test'] = 'test'; 
echo $array['test']; 

对象:

$object = new stdClass(); 
$object->test = 'test'; 
echo $object->test;