【问题标题】:sorting the value of multidimensional array by key按键对多维数组的值进行排序
【发布时间】:2012-11-27 16:35:50
【问题描述】:

表格

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
                        )

                )

        )
)

【问题讨论】:

  • 很确定我今天早上已经回答了这个人的这个问题。
  • 先看我的问题,我给你的第一个问题和这个不一样

标签: php


【解决方案1】:

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

【讨论】:

  • 您也许可以添加更多详细信息。但你得到了我对该原则的投票。
【解决方案2】:

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

【讨论】:

【解决方案3】:

您可以使用 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"]);
}

【讨论】:

  • 什么是 O 和 C 数组对不起我是新手
  • O(1) 是一种表示法,意味着您可以在恒定数量的操作中执行一些操作,无论值如何。 O(n) 表示您执行 k*n+c 指令“n”是您问题的参数。 “C”只是一种语言。在这种语言中,数组是“真正的”数组,没有“键值”关联的概念。因此,您总是使用搜索函数来查找索引,然后访问该值以对数组进行排序。在 php 中,有了这个 key 的想法,你就可以避免搜索。
  • 但我需要该设计来构建我的下拉列表
  • 一点也不。下拉列表可以使用键作为名称或值。如果你想绝对使用索引,你可以使用一个变量来计算对象的数量。这是不被禁止的。
  • 就像我说的,这是一个 hack,不是很干净,但它可以工作。事实上,我使用 uksort 并给出一个 Closure(php 5.3 强制)作为比较函数。它不是压倒一切的,它只是使用该功能。我声明了一个闭包(一个可以使用全局变量的匿名函数)并将它交给 uksort
猜你喜欢
  • 1970-01-01
  • 2013-02-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-15
相关资源
最近更新 更多