2011-03-22 52 views
0

我需要修改一个对象(一个Wordpress对象)的属性,我想通过使用匹配的值数组来完成它。php:修改匹配数组的对象属性

WordPress的对象

array(9) { 
    ["post"]=> object(stdClass)#120 (25) { 
    ["labels"]=> object(stdClass)#123 (12) { 
     ["name"]=> string(5) "Posts" 
     ["singular_name"]=> string(4) "Post" 
     ["add_new"]=> string(7) "Add New" 
     ["add_new_item"]=> string(12) "Add New Post" 
     ["edit_item"]=> string(9) "Edit Post" 
     ["new_item"]=> string(8) "New Post" 
     ["view_item"]=> string(9) "View Post" 
     ["search_items"]=> string(12) "Search Posts" 
     ["not_found"]=> string(15) "No posts found." 
     ["not_found_in_trash"]=> string(24) "No posts found in Trash." 
     ["parent_item_colon"]=> NULL 
     ["menu_name"]=> string(5) "Posts" 
    } 
    ["description"]=> string(0) "" 
    ["publicly_queryable"]=> bool(true) 
    ["exclude_from_search"]=> bool(false) 
    ["capability_type"]=> string(4) "post" 
    ["map_meta_cap"]=> bool(true) 
    ["_builtin"]=> bool(true) 
    ["_edit_link"]=> string(16) "post.php?post=%d" 
    ["hierarchical"]=> bool(false) 
    ["public"]=> bool(true) 
    ["rewrite"]=> bool(false) 
    ["has_archive"]=> bool(false) 
    ["query_var"]=> bool(false) 
    ["register_meta_box_cb"]=> NULL 
    ["taxonomies"]=> array(0) { 
    } 
    ["show_ui"]=> bool(true) 
    ["menu_position"]=> NULL 
    ["menu_icon"]=> NULL 
    ["permalink_epmask"]=> int(1) 
    ["can_export"]=> bool(true) 
    ["show_in_nav_menus"]=> bool(true) 
    ["show_in_menu"]=> bool(true) 
    ["name"]=> string(4) "post" 
    ["cap"]=> object(stdClass)#122 (14) { 
     ["edit_post"]=> string(9) "edit_post" 
     ["read_post"]=> string(9) "read_post" 
     ["delete_post"]=> string(11) "delete_post" 
     ["edit_posts"]=> string(10) "edit_posts" 
     ["edit_others_posts"]=> string(17) "edit_others_posts" 
     ["publish_posts"]=> string(13) "publish_posts" 
     ["read_private_posts"]=> string(18) "read_private_posts" 
     ["read"]=> string(4) "read" 
     ["delete_posts"]=> string(12) "delete_posts" 
     ["delete_private_posts"]=> string(20) "delete_private_posts" 
     ["delete_published_posts"]=> string(22) "delete_published_posts" 
     ["delete_others_posts"]=> string(19) "delete_others_posts" 
     ["edit_private_posts"]=> string(18) "edit_private_posts" 
     ["edit_published_posts"]=> string(20) "edit_published_posts" 
    } 
    ["label"]=> string(5) "Posts" 
    } 
etc..., 
etc..., 
} 

值的数组改变

$newpost = array(
    'post' => array (
     'labels' => array(
     'name' => 'News', 
     'singular_name' => 'News', 
     'add_new' => 'Add New', 
     'add_new_item' => 'Add New News', 
     'edit_item' => 'Edit News', 
     'new_item' => 'New News', 
     'view_item' => 'View News', 
     'search_items' => 'Search News', 
     'not_found' => 'No news found', 
     'not_found_in_trash' => 'No news found in Trash', 
     'parent_item_colon' => '', 
     'menu_name' => 'News' 
    ), 
    'menu_icon' => get_bloginfo('template_url').'/template/cup.png', 
    ) 
); 

这可能吗?

+0

想改变只''标签'数组的值 – diEcho 2011-03-22 10:30:38

回答

0

快速&肮脏的解决方案来到我的脑海:

我用json_decode和编码,因为铸造原始对象使用(array)$wordpressObject不会转换一个数组嵌套对象数组

$wordpressArray = json_decode(json_encode($wordpressObject), true); 

$modifiedArray = array_merge($wordpressArray, $newpost); 

JSON序列化再次用于在数组和对象之间进行递归转换

$modifiedWordpressObject = json_decode(json_encode($modifiedArray)); 

我刚写了一个例子在codepad

这将是一个更清洁的解决方案,写一个递归算法将数组转换为对象,反过来,但这是我想到的第一件事。

+0

我忘了提及我以两种截然不同的方式使用json_decode:将第二个参数'true'传递给它将使函数将json转换为关联数组,同时省略它将返回嵌套的stdClass(object)结构。 – 2011-03-22 10:44:43

0

您可以尝试使用array_merge()将第二个数组合并到第一个数组中,并且第二个数组中的任何重复值将覆盖第一个数组中的值。

+0

你发布的解决方案几乎没问题,但问题是被覆盖的变量不是数组,而是stdClass的嵌套对象结构。 – 2011-03-22 10:41:40