用某些值排序数组?

时间:2012-09-30 14:23:18

标签: php arrays grouping

这是从mysql查询推送的输出数组

Array
(
    [0] => Array
        (
            [CategoryID] => 7
            [CategoryName] => Test it
            [Parent] => 2
            [Thumb] => 4
            [sort] => 0
            [MediaID] => 4
            [MediaTitle] => asd
            [MediaName] => 1349012691_4.jpg
            [MediaType] => image
            [MediaSize] => 369848
            [UploadTime] => 1349012691
        )

    [1] => Array
        (
            [CategoryID] => 8
            [CategoryName] => Test all
            [Parent] => 4
            [Thumb] => 5
            [sort] => 0
            [MediaID] => 5
            [MediaTitle] => asaas
            [MediaName] => 1349012728_9.jpg
            [MediaType] => image
            [MediaSize] => 416817
            [UploadTime] => 1349012728
        )

    [2] => Array
        (
            [CategoryID] => 4
            [CategoryName] => Test Image
            [Parent] => 0
            [Thumb] => 0
            [sort] => 2
            [MediaID] => 
            [MediaTitle] => 
            [MediaName] => 
            [MediaType] => 
            [MediaSize] => 
            [UploadTime] => 
        )

    [3] => Array
        (
            [CategoryID] => 2
            [CategoryName] => Test Sub
            [Parent] => 1
            [Thumb] => 0
            [sort] => 4
            [MediaID] => 
            [MediaTitle] => 
            [MediaName] => 
            [MediaType] => 
            [MediaSize] => 
            [UploadTime] => 
        )

    [4] => Array
        (
            [CategoryID] => 3
            [CategoryName] => Test Category
            [Parent] => 0
            [Thumb] => 0
            [sort] => 6
            [MediaID] => 
            [MediaTitle] => 
            [MediaName] => 
            [MediaType] => 
            [MediaSize] => 
            [UploadTime] => 
        )

    [5] => Array
        (
            [CategoryID] => 1
            [CategoryName] => Test Category
            [Parent] => 0
            [Thumb] => 0
            [sort] => 8
            [MediaID] => 
            [MediaTitle] => 
            [MediaName] => 
            [MediaType] => 
            [MediaSize] => 
            [UploadTime] => 
        )

    [6] => Array
        (
            [CategoryID] => 5
            [CategoryName] => Test Category
            [Parent] => 0
            [Thumb] => 0
            [sort] => 10
            [MediaID] => 
            [MediaTitle] => 
            [MediaName] => 
            [MediaType] => 
            [MediaSize] => 
            [UploadTime] => 
        )

    [7] => Array
        (
            [CategoryID] => 6
            [CategoryName] => Test Remove
            [Parent] => 0
            [Thumb] => 0
            [sort] => 12
            [MediaID] => 
            [MediaTitle] => 
            [MediaName] => 
            [MediaType] => 
            [MediaSize] => 
            [UploadTime] => 
        )

)

我需要对此数组进行排序,以便显示“父类别优先”和“子类别”。

我需要像

这样的输出
Array
(

    [2] => Array
        (
            [CategoryID] => 4
            [CategoryName] => Test Image
            [Parent] => 0
            [Thumb] => 0
            [sort] => 2
            [MediaID] => 
            [MediaTitle] => 
            [MediaName] => 
            [MediaType] => 
            [MediaSize] => 
            [UploadTime] => 
        )
        //sub category
            [1] => Array
            (
                [CategoryID] => 8
                [CategoryName] => Test all
                [Parent] => 4
                [Thumb] => 5
                [sort] => 0
                [MediaID] => 5
                [MediaTitle] => asaas
                [MediaName] => 1349012728_9.jpg
                [MediaType] => image
                [MediaSize] => 416817
                [UploadTime] => 1349012728
            )

    [3] => Array
        (
            [CategoryID] => 2
            [CategoryName] => Test Sub
            [Parent] => 1
            [Thumb] => 0
            [sort] => 4
            [MediaID] => 
            [MediaTitle] => 
            [MediaName] => 
            [MediaType] => 
            [MediaSize] => 
            [UploadTime] => 
        )
                //sub category
                [0] => Array
                (
                    [CategoryID] => 7
                    [CategoryName] => Test it
                    [Parent] => 2
                    [Thumb] => 4
                    [sort] => 0
                    [MediaID] => 4
                    [MediaTitle] => asd
                    [MediaName] => 1349012691_4.jpg
                    [MediaType] => image
                    [MediaSize] => 369848
                    [UploadTime] => 1349012691
                )

    [4] => Array
        (
            [CategoryID] => 3
            [CategoryName] => Test Category
            [Parent] => 0
            [Thumb] => 0
            [sort] => 6
            [MediaID] => 
            [MediaTitle] => 
            [MediaName] => 
            [MediaType] => 
            [MediaSize] => 
            [UploadTime] => 
        )

    [5] => Array
        (
            [CategoryID] => 1
            [CategoryName] => Test Category
            [Parent] => 0
            [Thumb] => 0
            [sort] => 8
            [MediaID] => 
            [MediaTitle] => 
            [MediaName] => 
            [MediaType] => 
            [MediaSize] => 
            [UploadTime] => 
        )

    [6] => Array
        (
            [CategoryID] => 5
            [CategoryName] => Test Category
            [Parent] => 0
            [Thumb] => 0
            [sort] => 10
            [MediaID] => 
            [MediaTitle] => 
            [MediaName] => 
            [MediaType] => 
            [MediaSize] => 
            [UploadTime] => 
        )

    [7] => Array
        (
            [CategoryID] => 6
            [CategoryName] => Test Remove
            [Parent] => 0
            [Thumb] => 0
            [sort] => 12
            [MediaID] => 
            [MediaTitle] => 
            [MediaName] => 
            [MediaType] => 
            [MediaSize] => 
            [UploadTime] => 
        )

)

我可以用PHP做到吗?

3 个答案:

答案 0 :(得分:1)

你可以尝试

# Short Version of your Array
$oldList = Array(
    "0" => Array("CategoryID" => 7,"CategoryName" => "Test it","Parent" => 2),
    "1" => Array("CategoryID" => 8,"CategoryName" => "Test all","Parent" => 4),
    "2" => Array("CategoryID" => 4,"CategoryName" => "Test Image","Parent" => 0),
    "3" => Array("CategoryID" => 2,"CategoryName" => "Test Sub","Parent" => 1),
    "4" => Array("CategoryID" => 3,"CategoryName" => "Test Category","Parent" => 0),
    "5" => Array("CategoryID" => 1,"CategoryName" => "Test Category","Parent" => 0),
    "6" => Array("CategoryID" => 5,"CategoryName" => "Test Category","Parent" => 0),
    "7" => Array("CategoryID" => 6,"CategoryName" => "Test Remove","Parent" => 0)
);

echo "<pre>";
print_r(__group($oldList));

使用的功能

function __group($oldList)
{
    # Get Category Position
    $category = array_map(function($item) { return $item['CategoryID'];}  ,$oldList); 

    #Get Only Parent List and add to new list
    $newList = array_filter($oldList , function($item) {  return $item['Parent']  == 0 ; } );

    foreach($oldList as $key => $value)
    {
        if($value['Parent'] != 0) {
            # Mapt Chiled to parent using Parent ID & Category ID 
            $newList[array_search($value['Parent'],$category)]['child'][] = $value;
        }
    }
    return $newList ;
}

输出

Array
(
    [2] => Array
        (
            [CategoryID] => 4
            [CategoryName] => Test Image
            [Parent] => 0
            [child] => Array
                (
                    [0] => Array
                        (
                            [CategoryID] => 8
                            [CategoryName] => Test all
                            [Parent] => 4
                        )

                )

        )

    [4] => Array
        (
            [CategoryID] => 3
            [CategoryName] => Test Category
            [Parent] => 0
        )

    [5] => Array
        (
            [CategoryID] => 1
            [CategoryName] => Test Category
            [Parent] => 0
            [child] => Array
                (
                    [0] => Array
                        (
                            [CategoryID] => 2
                            [CategoryName] => Test Sub
                            [Parent] => 1
                        )

                )

        )

    [6] => Array
        (
            [CategoryID] => 5
            [CategoryName] => Test Category
            [Parent] => 0
        )

    [7] => Array
        (
            [CategoryID] => 6
            [CategoryName] => Test Remove
            [Parent] => 0
        )

    [3] => Array
        (
            [child] => Array
                (
                    [0] => Array
                        (
                            [CategoryID] => 7
                            [CategoryName] => Test it
                            [Parent] => 2
                        )

                )

        )

)

答案 1 :(得分:0)

这会根据需要对条目进行分组:

function groupData($data) {
    $tree = array();
    $waiting = array();
    $index = array();
    foreach ($data as &$entry) {
        $index[$entry['CategoryID']] = &$entry;
        if (isset($waiting[$entry['CategoryID']])) {
            $entry['children'] = &$waiting[$entry['CategoryID']];
        } else {
            $entry['children'] = array();
        }
        if ($entry['Parent']) {
            if (isset($index[$entry['Parent']])) {
                $index[$entry['Parent']]['children'][] = &$entry;
            } else {
                if (!isset($waiting[$entry['Parent']])) {
                    $waiting[$entry['Parent']] = array();
                }
                $waiting[$entry['Parent']][] = &$entry;
            }
        } else {
            $tree[] = &$entry;
        }
    }
    return $tree;
}

如果您还需要按'sort'属性对结果进行排序,那么最好的一些名称或其他内容将在您的SQL查询中执行此操作。上述功能不会改变记录顺序。

答案 2 :(得分:0)

您可以使用以下代码。这将根据排序键对每个数组(根数组和子数组)进行分组和排序。要检索新的分组/排序数组,请使用此$ newArray = groupMe($ originalArray);

function groupMe($array) {
    $returningArray = array();

    $mapCatIdCategory = array();
    //first pass create mapCatIdCategory
    foreach ($array as &$item) {
        $mapCatIdCategory[$item['CategoryId']] = &$item;
        $item['child'] = array();
    }

    //second pass is for setting child
    foreach ($array as &$item) {
        $parentId = $item['Parent'];
        if ($parentId == 0) {   //no parent category => add to root
            $returningArray[] = &$item;
        }
        else if (isset($mapCatIdCategory[$parentId])){ //not sure if data is always valid
            $mapCatIdCategory[$parentId]['child'][] = &$item; 
        }
    }

    //third pass sort root and all child array(recursively)
    recursiveSort($returningArray);

    return $returningArray;
}

function recursiveSort(&$array) {
    foreach ($array as &$item) {
        if (count($item['child']) >  1) { //empty or 1 elements arrays are already sorted
            recursiveSort($item['child']); //sort child
        }
    }

    usort($array, "cmpCategory"); //sort array by user comparasion function
}

function cmpCategory($a, $b) {
    if ($a['sort'] == $b['sort']) {
        return 0;
    }
    return ($a['sort'] < $b['sort']) ? -1 : 1;
}