简体   繁体   English

按键排序多维数组的值

[英]sorting the value of multidimensional array by key

table

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


Im trying to get the categories in the db and put it in an array then after use that array to build the code for the drop down list. 我试图获取数据库中的类别并将其放入数组然后使用该数组后,为下拉列表构建代码。 Here is the code BELOW. 这是代码BELOW。

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";
        }


    }


final value of $categories array $ 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
                (
                )

        )

)


But I want to sort the $categories by the value of the key 'name'. 但我想按键'name'的值对$ categories进行排序。

I tried many times and so far this is my solution. 我尝试了很多次,到目前为止这是我的解决方案。

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

        }
    }
}


My problem is even that the first level key 'name' sorts fine, but my solution to sort the second level didnt work. 我的问题是,即使第一级关键'名称'排序很好,但我对第二级排序的解决方案不起作用。

And also when the sorting happens the index of the array i sorted become 0 1 2 3. 而且当排序发生时,我排序的数组的索引变为0 1 2 3。

like for example when i sort this by the value of the key 'name' 18 12 13 will become 0 1 2 例如,当我按键'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


        )            
)

how can i sort but I also want 18 12 13 not change? 我该如何排序,但我也希望18 12 13不改变?




so I want my $categories array to be this 所以我希望我的$ 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
                        )

                )

        )
)

Too much effort. 付出太多努力。 Use NAME as array KEY and then sort by key wit ksort 使用NAME作为数组KEY,然后按键ksort排序

I think the issue is that PHP doesn't interpret those numerical indexes as being keys, just indices, so that it doesn't retain them after the sort. 我认为问题是PHP不会将这些数字索引解释为键,只是索引,因此在排序后它不会保留它们。 If you convert them to strings and then do the sort, I suspect it will keep them. 如果你将它们转换为字符串,然后进行排序,我怀疑它会保留它们。 Alternatively, you could swap the numerical keys with your names (if names are unique, of course), do a simple ksort (as suggested in previous answers), and then swap them back afterwards. 或者,您可以将数字键与您的名称交换(当然,如果名称是唯一的),请执行简单的ksort(如前面的答案所示),然后再将它们交换回来。

you an use uksort to perform what you want. 你使用uksort来执行你想要的。 But there is something wrong in your design. 但是你的设计有问题。

Like @Lukasz Kujawa said, php arrays are designed to be accessed in O(1). 就像@Lukasz Kujawa所说,php数组被设计为在O(1)中访问。 Your design, as C arrays, force everything to be O(n) and to add a step in the process. 您的设计作为C数组,强制所有内容为O(n)并在过程中添加一个步骤。

I will give you the "solution" with uksort but to me it is not something good to do. 我会用uksort给你“解决方案”,但对我来说这不是一件好事。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM