用另一个数组中的值填充数组

时间:2014-03-11 作者:Cookie_Pa

我已检索到WP\\u Post对象,并将其存储在变量中。

Array
(
[0] => WP_Post Object
    (
        [ID] => 45
        [post_author] => 1
        [post_date] => 2014-03-10 14:22:01
        [post_date_gmt] => 2014-03-10 14:22:01
        [post_content] => 
        [post_title] => sddddddddddd
        [post_excerpt] => 
        [post_status] => publish
        [comment_status] => closed
        [ping_status] => closed
        [post_password] => 
        [post_name] => sddddddddddd
        [to_ping] => 
        [pinged] => 
        [post_modified] => 2014-03-10 14:23:57
        [post_modified_gmt] => 2014-03-10 14:23:57
        [post_content_filtered] => 
        [post_parent] => 0
        [guid] => http://localhost/bitbucket/?post_type=advertisement&p=45
        [menu_order] => 0
        [post_type] => advertisement
        [post_mime_type] => 
        [comment_count] => 0
        [filter] => raw
    )

[1] => WP_Post Object
    (
        [ID] => 40
        [post_author] => 1
        [post_date] => 2014-03-10 11:00:28
        [post_date_gmt] => 2014-03-10 11:00:28
        [post_content] => 
        [post_title] => 123
        [post_excerpt] => 
        [post_status] => publish
        [comment_status] => closed
        [ping_status] => closed
        [post_password] => 
        [post_name] => 123
        [to_ping] => 
        [pinged] => 
        [post_modified] => 2014-03-10 12:49:24
        [post_modified_gmt] => 2014-03-10 12:49:24
        [post_content_filtered] => 
        [post_parent] => 0
        [guid] => http://localhost/bitbucket/?post_type=advertisement&p=40
        [menu_order] => 0
        [post_type] => advertisement
        [post_mime_type] => 
        [comment_count] => 0
        [filter] => raw
    )

[2] => WP_Post Object
    (
        [ID] => 37
        [post_author] => 1
        [post_date] => 2014-03-05 13:56:15
        [post_date_gmt] => 2014-03-05 13:56:15
        [post_content] => 
        [post_title] => asd
        [post_excerpt] => 
        [post_status] => publish
        [comment_status] => closed
        [ping_status] => closed
        [post_password] => 
        [post_name] => asd
        [to_ping] => 
        [pinged] => 
        [post_modified] => 2014-03-10 14:26:23
        [post_modified_gmt] => 2014-03-10 14:26:23
        [post_content_filtered] => 
        [post_parent] => 0
        [guid] => http://bitbucket/?post_type=advertisement&p=37
        [menu_order] => 0
        [post_type] => advertisement
        [post_mime_type] => 
        [comment_count] => 0
        [filter] => raw
    )
()

以上是我的目标。现在,我需要将其中的一些值存储在自定义数组中,该数组如下所示:

$my_array = array (
               ID => post_title
            );
ID和post\\u标题应来自上面的对象。我如何才能做到这一点?

1 个回复
最合适的回答,由SO网友:Rikesh 整理而成

您可以轻松使用foreach 为了实现这一目标,

$my_array = array();
foeach($wpArray as $row){
   $my_array[$row->ID] = $row->post_name;
}

结束

相关推荐

Count post views in loop

我正在尝试显示博客页面上每个帖子的帖子视图(所以在循环中)。我在函数中使用以下代码进行了尝试。php: // function to count views. function setAndViewPostViews($postID) { $count_key = \'views\'; $count = get_post_meta($postID, $count_key, true); if($count==\'\'){ $