如何将包含值的对象数组转换为由该值索引的数组


How to convert array of objects containing a value into an array indexed by that value?

我的数组结构是这样的:

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
    )
)
    创建一个新的空数组。
  1. 在循环中遍历原始数组。
  2. 获取循环中当前元素的menu_order键。
  3. 以menu_order为键将当前元素存储在新数组中。
  4. 循环结束后,销毁原数组。

例如:

<?php
$newArray = array();
foreach($oldArray as $element)
{
    $menuKey = (string) $element->menu_order;
    $newArray[$menuKey] = $element;
}
$oldArray = null;
 // $newArray now has required data 
?>

}

我有答案了

    foreach($whyapptivo_sub_pages as $pages)
    {
            $key = $pages->menu_order;
            $final[$key] = $pages;
            ksort($final);
        }
   foreach($final as $page)
    {
             ........
          }

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