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

时间:2011-09-15 04:54:15

标签: php arrays object key

我的数组结构如下:

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

4 个答案:

答案 0 :(得分:0)

  1. 创建一个新的空数组。
  2. 循环遍历原始数组。
  3. 循环中当前元素的retreive menu_order键。
  4. 将当前元素存储在新数组中,并将menu_order作为键。
  5. 循环后,销毁原始数组。
  6. 类似的东西:

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

Demo