php:使用匹配的数组修改对象属性
我需要修改一个对象(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',
)
);
这可能吗?
I need to modify properties of an object (a Wordpress object) and I would like to do it by using a matching array of values.
Wordpress Object
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...,
}
Array of values to be changed
$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',
)
);
Is this possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以尝试使用 array_merge() 它将结合将第二个数组放入第一个数组中,第二个数组中的任何重复值都会覆盖第一个数组中的值。
You could try to use array_merge() which will combine the second array into the first, and any duplicate values in the second array overwrite the values in the first array.
我想到了一个快速而肮脏的解决方案:
我使用 json_decode 并编码,因为使用
(array)$wordpressObject
将原始对象转换为数组不会将嵌套对象转换为数组json 序列化再次用于在数组和对象之间递归地转换
我刚刚在 codepad 上写了一个示例,
编写递归算法将是一个更干净的解决方案将数组转换为对象,反之亦然,但这是我首先想到的。
A quick&dirty solution came to my mind:
I use json_decode and encode because casting the original object to an array using
(array)$wordpressObject
won't convert nested objects to arraysjson serialization is used again to convert between array and object recursively
I just wrote an example on codepad
It would be a cleaner solution to write a recursive algorithm to convert arrays to objects, and the other way around, but it is the first thing that came to my mind.