php重新排序数组

时间:2011-10-11 08:10:10

标签: php arrays sorting grouping

我有一系列条目按年份和月份分隔,如下所示。我需要对这个数组进行排序,以便条目按年份分组,然后按类别分组。

Array
(
    [Oct 2011] => Array
        (
            [0] => Array
                (
                    [title] => CAD Drawing Updates
                    [file] => /gm-June2010-driver.pdf
                    [category] => Windows
                )
        )


    [Sep 2011] => Array
        (
            [0] => Array
                (
                    [title] => title
                    [file] => /gm-June2010-driver.pdf
                    [category] => Windows
                )

            [1] => Array
                (
                    [title] => edges
                    [file] => /gm-June2010-driver.pdf
                    [category] =>Walling
                )

            [2] => Array
                (
                    [title] => Specification update
                    [file] => /gm-June2010-driver.pdf
                    [category] => Windows
                )
        )
)

所以这就是我追求的那种东西。

Array
(
    [Oct 2011] => Array
        (
                [Windows] => Array
                        (
                            [0] => Array
                                (
                                    [title] => CAD Drawing Updates
                                    [file] => /gm-June2010-driver.pdf
                                )                       
                        )
        )


    [Sep 2011] => Array
        (
                [Windows] => Array
                                (
                            [0] => Array
                                (
                                    [title] => title
                                    [file] => /gm-June2010-driver.pdf
                                    [category] => Windows
                                )


                            [1] => Array
                                (
                                    [title] => Specification update
                                    [file] => /gm-June2010-driver.pdf
                                    [category] => Windows
                                )                                                               
                                )   
                [Walling] => Array
                                (
                            [0] => Array
                                (
                                    [title] => edges
                                    [file] => /gm-June2010-driver.pdf
                                    [category] => Curtain Walling
                                )                                                               
                                )                                            
        )
)

我不确定这是否是排序功能的工作,任何帮助都将不胜感激,谢谢。

3 个答案:

答案 0 :(得分:1)

不,使用sort函数无法完成,您需要通过使用嵌套的foreach循环遍历原始数组来创建新数组。

$newArr = array();
foreach($arr as $month => items) {
  foreach($items as $data) {
    $newArr[$month][$data["category"]][] = $data;
  }
}

答案 1 :(得分:0)

您不想排序,只是重命名键。 PHP Arrays不支持直接重命名密钥,因此您需要首先创建一个使用新密钥构建的新数组,同时保持现有值。

最后,您可以用新的数组替换原始数组:

$rekeyed = array();
foreach($array as $monthYear => &$value)
{
    $r = sscanf($monthYear, '%s %d', $month, $year);
    if ($r != 2) throw new Exception(sprintf('Invalid Key "%s".', $monthYear));
    $categories = array();
    foreach($value as &$item)
    {
        $category =& $item['category'];
        unset($item['category']);
        $categories[$category][] =& $item;
    }
    unset($item);
    $value =& $categories;
    unset($categories);    
    $rekeyed[$year][$month] =& $value;
}
unset($value);
$array =& $rekeyed;
unset($rekeyed);
print_r($array);

输出:

Array
(
    [2011] => Array
        (
            [Oct] => Array
                (
                    [Windows] => Array
                        (
                            [0] => Array
                                (
                                    [title] => CAD Drawing Updates
                                    [file] => /gm-June2010-driver.pdf
                                )

                        )

                )

            [Sep] => Array
                (
                    [Windows] => Array
                        (
                            [0] => Array
                                (
                                    [title] => title
                                    [file] => /gm-June2010-driver.pdf
                                )

                        )

                    [Walling] => Array
                        (
                            [0] => Array
                                (
                                    [title] => edges
                                    [file] => /gm-June2010-driver.pdf
                                )

                        )

                )

        )

)

Demo

答案 2 :(得分:-1)

我认为你应该关注的功能是uasort。

http://www.php.net/manual/en/function.uasort.php

首先对主数组进行排序,对于数组中的每个元素,对子数组执行一次操作。