开发者

php re-sorting an array

I have an array of entries that are separated by year and month like below. I need to sort this array so that entries are "grouped" by year and then by category.

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

So this is the sort of thing I'm after.

Array
(
    [Oct 2011] => Array
        (
                [Windows] => Array
                        (
                            [0] => Array
                                (
                                    [title] => CAD Drawing Updates
                                    [file] => /gm-June2010-driver.pdf
                                )                       
  开发者_C百科                      )
        )


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

I'm not sure if this is a job for the sort functions, any help would be appreciated, thanks.


No, that can't be done with the sort function, you need to create a new array by iterating through your original array with nested foreach loops.

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


You don't want to sort but just to rename keys. PHP Arrays do not support to rename keys directly, so you need to create a new array first that is build with the new keys while maintaining the existing values.

At the end you can replace the original array with the new one:

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

Output:

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


I think the function you should be looking at is uasort.

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

First do a sort on the main array, the for each element in the array, do a usort on the child array.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜