0

桌子

cat_id | cat_parent_id | cat_name   
---------------------------------
12     |      0        |  Car    
13     |      0        |  Manga      
14     |      12       |  Volvo      
15     |      12       |  Mercedes-Benz      
16     |      13       |  Naruto     
17     |      13       |  Hunter X Hunter
18     |      0        |  Animals


我试图获取数据库中的类别并将其放入一个数组中,然后使用该数组构建下拉列表的代码。这是下面的代码。

while($row = dbFetchArray($result)) {
        list($id, $parentId, $name) = $row;

        if ($parentId == 0) {
            // we create a new array for each top level categories
            $categories[$id] = array('id' => $id, 'name' => $name, 'children' => array());
        } else {
            // the child categories are put int the parent category's array
            $categories[$parentId]['children'][] = array('id' => $id, 'name' => $name); 
        }
}

$list = '';
    foreach ($categories as $key => $value) {
        $name     =  "--" . strtoupper($value['name']) . "--";
        $children = $value['children'];

        $list .="<option value=\"{$value['id']}\"";
        if ($value['id'] == $catId) {
                $list.= " selected";
        }
        $list .= ">$name</option>\r\n";


        foreach ($children as $child) {
            $list .= "<option value=\"{$child['id']}\"";
            if ($child['id'] == $catId) {
                $list.= " selected";
            }

            $list .= ">{$child['name']}</option>\r\n";
        }


    }


$categories 数组的最终值

Array
(
    [12] => Array
        (
            [id] => 12
            [name] => Car
            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 14
                            [name] => Volvo
                        )

                    [1] => Array
                        (
                            [id] => 15
                            [name] => Mercedez-Benz
                        )

                )

        )

    [13] => Array
        (
            [id] => 13
            [name] => Manga
            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 16
                            [name] => Naruto
                        )

                    [1] => Array
                        (
                            [id] => 17
                            [name] => Hunter X Hunter
                        )

                )

        )

    [18] => Array
        (
            [id] => 18
            [name] => Animals
            [children] => Array
                (
                )

        )

)


但我想按键“名称”的值对 $categories 进行排序。

我尝试了很多次,到目前为止这是我的解决方案。

function sortByName($a, $b) {
    return strcmp($a["name"], $b["name"]);
}

usort($categories,"sortByName");

foreach($categories as $k => $v){
    foreach($v as $kk => $vv) {

        if ($kk == "children") {
            usort($vv,"sortByName");

            print_r($vv );

        }
    }
}


我的问题甚至是第一级键“名称”排序很好,但我对第二级进行排序的解决方案不起作用。

而且当排序发生时,我排序的数组的索引变为0 1 2 3。

例如,当我按键 'name' 18 12 13 的值对其进行排序时,将变为 0 1 2

Array(
    [18] => Array
        (
            [id] => 18
            [name] => aa


        )

    [12] => Array
        (
            [id] => 12
            [name] => Car


        )

    [13] => Array
        (
            [id] => 13
            [name] => Manga


        )            
)

我该如何排序,但我也希望 18 12 13 不改变?




所以我希望我的 $categories 数组是这样的

Array
(

    [18] => Array
        (
            [id] => 18
            [name] => Animals
            [children] => Array
                (
                )

        )

    [12] => Array
        (
            [id] => 12
            [name] => Car
            [children] => Array
                (
            [0] => Array
                        (
                            [id] => 15
                            [name] => Mercedes-Benz
                        )

                    [1] => Array
                        (
                            [id] => 14
                            [name] => Volvo
                        )

            )

        )

    [13] => Array
        (
            [id] => 13
            [name] => Manga
            [children] => Array
                (
            [0] => Array
                        (
                            [id] => 17
                            [name] => Hunter X Hunter
                        )

                    [1] => Array
                        (
                            [id] => 16
                            [name] => Naruto
                        )

                )

        )
)
4

3 回答 3

2

太费劲了。使用 NAME 作为数组 KEY,然后用ksort键排序

于 2012-11-27T16:38:09.847 回答
0

我认为问题在于 PHP 不会将这些数字索引解释为键,而只是索引,因此它不会在排序后保留它们。如果您将它们转换为字符串然后进行排序,我怀疑它会保留它们。或者,您可以将数字键与您的姓名交换(当然,如果名称是唯一的),做一个简单的 ksort (如前面的答案中所建议的那样),然后再将它们交换回来。

于 2012-11-27T16:43:27.337 回答
0

您可以使用 uksort 来执行您想要的操作。但是你的设计有问题。

就像@Lukasz Kujawa 所说,php 数组被设计为在 O(1) 中访问。您的设计,作为 C 数组,强制所有内容为 O(n) 并在过程中添加一个步骤。

我会给你 uksort 的“解决方案”,但对我来说这不是一件好事。

uksort($categories,function($a,$b)use($categories){
       return return strcmp($categories[$a]["name"], $categories[$b]["name"]);
}
于 2012-11-27T16:44:57.230 回答