如何将包含值的对象数组转换为由该值索引的数组?
我的数组结构如下:
Array
(
[0] => stdClass Object
(
[ID] => 277
[post_author] => 1
[post_date] => 2011-09-02 08:34:03
[post_date_gmt] => 2011-09-02 08:34:03
[post_content] => <div class="sol_topcont">
[menu_order] => 103
[post_type] => page
[post_mime_type] =>
[comment_count] => 0
[filter] => raw
)
[1] => stdClass Object
(
[ID] => 275
[post_author] => 1
[post_date] => 2011-09-02 08:32:36
[post_date_gmt] => 2011-09-02 08:32:36
[post_content] => <div class="sol_topcont1">
[menu_order] => 100
[post_type] => page
[post_mime_type] =>
[comment_count] => 0
[filter] => raw
)
[2] => stdClass Object
(
[ID] => 280
[post_author] => 1
[post_date] => 2011-09-02 08:35:24
[post_date_gmt] => 2011-09-02 08:35:24
[post_content] => <div class="sol_topcont">
[menu_order] => 102
[post_type] => page
[post_mime_type] =>
[comment_count] => 0
[filter] => raw
)
[3] => stdClass Object
(
[ID] => 282
[post_author] => 1
[post_date] => 2011-09-02 08:36:31
[post_date_gmt] => 2011-09-02 08:36:31
[post_content] => <div class="sol_topcont">
[menu_order] => 101
[post_type] => page
[post_mime_type] =>
[comment_count] => 0
[filter] => raw
)
)
我需要使用 ['menu_order'] 键值对该数组进行排序。如何将上面的数组转换为这样的数组:
Array
(
[100] => stdClass Object
(
[ID] => 275
[post_author] => 1
[post_date] => 2011-09-02 08:32:36
[post_date_gmt] => 2011-09-02 08:32:36
[post_content] => <div class="sol_topcont1">
[menu_order] => 100
[post_type] => page
[post_mime_type] =>
[comment_count] => 0
[filter] => raw
)
[101] => stdClass Object
(
[ID] => 282
[post_author] => 1
[post_date] => 2011-09-02 08:36:31
[post_date_gmt] => 2011-09-02 08:36:31
[post_content] => <div class="sol_topcont">
[menu_order] => 101
[post_type] => page
[post_mime_type] =>
[comment_count] => 0
[filter] => raw
)
[102] => stdClass Object
(
[ID] => 280
[post_author] => 1
[post_date] => 2011-09-02 08:35:24
[post_date_gmt] => 2011-09-02 08:35:24
[post_content] => <div class="sol_topcont">
[menu_order] => 102
[post_type] => page
[post_mime_type] =>
[comment_count] => 0
[filter] => raw
)
[103] => stdClass Object
(
[ID] => 277
[post_author] => 1
[post_date] => 2011-09-02 08:34:03
[post_date_gmt] => 2011-09-02 08:34:03
[post_content] => <div class="sol_topcont">
[menu_order] => 103
[post_type] => page
[post_mime_type] =>
[comment_count] => 0
[filter] => raw
)
)
I have array structured like this:
Array
(
[0] => stdClass Object
(
[ID] => 277
[post_author] => 1
[post_date] => 2011-09-02 08:34:03
[post_date_gmt] => 2011-09-02 08:34:03
[post_content] => <div class="sol_topcont">
[menu_order] => 103
[post_type] => page
[post_mime_type] =>
[comment_count] => 0
[filter] => raw
)
[1] => stdClass Object
(
[ID] => 275
[post_author] => 1
[post_date] => 2011-09-02 08:32:36
[post_date_gmt] => 2011-09-02 08:32:36
[post_content] => <div class="sol_topcont1">
[menu_order] => 100
[post_type] => page
[post_mime_type] =>
[comment_count] => 0
[filter] => raw
)
[2] => stdClass Object
(
[ID] => 280
[post_author] => 1
[post_date] => 2011-09-02 08:35:24
[post_date_gmt] => 2011-09-02 08:35:24
[post_content] => <div class="sol_topcont">
[menu_order] => 102
[post_type] => page
[post_mime_type] =>
[comment_count] => 0
[filter] => raw
)
[3] => stdClass Object
(
[ID] => 282
[post_author] => 1
[post_date] => 2011-09-02 08:36:31
[post_date_gmt] => 2011-09-02 08:36:31
[post_content] => <div class="sol_topcont">
[menu_order] => 101
[post_type] => page
[post_mime_type] =>
[comment_count] => 0
[filter] => raw
)
)
I need to sort this array using the ['menu_order'] key value. How do I convert the above array into one like this:
Array
(
[100] => stdClass Object
(
[ID] => 275
[post_author] => 1
[post_date] => 2011-09-02 08:32:36
[post_date_gmt] => 2011-09-02 08:32:36
[post_content] => <div class="sol_topcont1">
[menu_order] => 100
[post_type] => page
[post_mime_type] =>
[comment_count] => 0
[filter] => raw
)
[101] => stdClass Object
(
[ID] => 282
[post_author] => 1
[post_date] => 2011-09-02 08:36:31
[post_date_gmt] => 2011-09-02 08:36:31
[post_content] => <div class="sol_topcont">
[menu_order] => 101
[post_type] => page
[post_mime_type] =>
[comment_count] => 0
[filter] => raw
)
[102] => stdClass Object
(
[ID] => 280
[post_author] => 1
[post_date] => 2011-09-02 08:35:24
[post_date_gmt] => 2011-09-02 08:35:24
[post_content] => <div class="sol_topcont">
[menu_order] => 102
[post_type] => page
[post_mime_type] =>
[comment_count] => 0
[filter] => raw
)
[103] => stdClass Object
(
[ID] => 277
[post_author] => 1
[post_date] => 2011-09-02 08:34:03
[post_date_gmt] => 2011-09-02 08:34:03
[post_content] => <div class="sol_topcont">
[menu_order] => 103
[post_type] => page
[post_mime_type] =>
[comment_count] => 0
[filter] => raw
)
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
像这样的东西:
}
Something like :
}
我得到了答案
I got an Answer
PHP 有一个
usort
函数,您可以将自定义比较器传递给其中。您需要创建一个具有两个参数(在本例中是两个对象)的函数,对它们进行比较,并根据比较结果返回 -1、0 或 1。http://www.php.net/manual/en/function.usort.php
示例
PHP has a
usort
function you can pass a custom comparator into. You will need to make a function that has two arguments (in this instance two of your objects), compare them, and return -1, 0 or 1 depending on the comparison.http://www.php.net/manual/en/function.usort.php
Example