0

这是从 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 做到这一点吗?

4

3 回答 3

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
                        )

                )

        )

)
于 2012-09-30T15:04:29.843 回答
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;
}

如果您还需要按“排序”属性对结果进行排序,则最好在 SQL 查询中使用某个名称或其他任何名称。上面的函数不会改变记录顺序。

于 2012-09-30T14:59:37.443 回答
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;
}
于 2012-09-30T15:32:48.247 回答