php:使用匹配的数组修改对象属性

时间:2011-03-22 10:16:59

标签: php arrays object

我需要修改对象的属性(Wordpress对象),我想通过使用匹配的数组值来实现。

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',
   )
);

这可能吗?

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分: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

上写了一个例子

编写递归算法将数组转换为对象是一种更清晰的解决方案,反之亦然,但这是我想到的第一件事。