我的数组结构如下:
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
)
)
答案 0 :(得分:0)
类似的东西:
<?php
$newArray = array();
foreach($oldArray as $element)
{
$menuKey = (string) $element->menu_order;
$newArray[$menuKey] = $element;
}
$oldArray = null;
// $newArray now has required data
?>
}
答案 1 :(得分:0)
PHP有一个usort
函数,您可以将自定义比较器传递给它。您需要创建一个具有两个参数的函数(在本例中为两个对象),比较它们,并根据比较返回-1,0或1。
http://www.php.net/manual/en/function.usort.php
示例强>
function cmp($a, $b) {
if ($a->menu_order == $b->menu_order) {
return 0;
}
return ($a->menu_order < $b->menu_order) ? -1 : 1;
}
usort($your_array, 'cmp');
答案 2 :(得分:0)
我得到了答案
foreach($whyapptivo_sub_pages as $pages)
{
$key = $pages->menu_order;
$final[$key] = $pages;
ksort($final);
}
foreach($final as $page)
{
........
}
答案 3 :(得分:0)
你也可以试试这个:
$arr = array(
array('menu_order' => 103, 'id' => 227),
array('menu_order' => 101, 'id' => 221),
array('menu_order' => 95, 'id' => 222),
array('menu_order' => 105, 'id' => 223),
array('menu_order' => 97, 'id' => 228),
array('menu_order' => 99, 'id' => 229));
function cmpObj($obj1, $obj2)
{
return $obj1->menu_order > $obj2->menu_order;
}
uasort($arr, 'cmpObj');
print_r($arr);