duanhuo3392 2015-04-30 10:33
浏览 37
已采纳

如何过滤关联数组?

I have to deal with a refactoring, to reduce the number of lines of code in PHP, to filter an associative array. So I'm making a select from DB in MySQL, to get an associative array. So my "Object" has a category and a surname field.

while ($row = mysqli_fetch_array($result)) {
        $array[] = $row['category'];
        $array[] = $row['Surname'];
   }

I want to obtain from this array, as many other sub-array, splitted by the category. I mean the category Array identification may be:

$categories = array("A","B","C","D");

So what I want, is to obtain one Array for each Category, which contains all the Surname, of that category. So suppose that the method works, something like that:

$arrayFiltered = method_filter($array_asso,"A");

At the end I want something like that:

    foreach ($categories as &$value) {
       $arrayFiltered = method_filter($array_asso,$value);
       my_method_which_needs_the_filtered_array($arrayFiltered);
}

Thank you in advance for your help.

  • 写回答

2条回答 默认 最新

  • dshp9580656 2015-04-30 10:42
    关注

    Sergeant's approach is the easiest. Just for the sake of it, here is an approach with array_filter() (just in case you have to have an unfiltered array as well):

    $array = [];
    $categories = array("A","B","C","D");
    
    while ($row = mysqli_fetch_array($result)) {
        $item = [
            'category' => $row['category'],
            'surname' => $row['Surname']
        ];
    
        $array[] = $item;
    }
    
    $categorized = [];
    
    foreach ($categories as $category) {
        $categorized[$category] = array_filter($array, function($item) use ($category) {
            return $item['category'] == $category;
        });
    }
    

    Here is a proof of concept without the need of a database connection:

    $categories = array("A","B","C","D");
    
    $array = [
        ['category' => 'A', 'Surname' => 'A Name 1'],
        ['category' => 'A', 'Surname' => 'A Name 2'],
        ['category' => 'B', 'Surname' => 'B Name 1'],
        ['category' => 'B', 'Surname' => 'B Name 2'],
        ['category' => 'B', 'Surname' => 'B Name 3'],
        ['category' => 'C', 'Surname' => 'C Name'],
    ];
    
    $categorized = [];
    
    foreach ($categories as $category) {
        $categorized[$category] = array_filter($array, function($item) use ($category) {
            return $item['category'] == $category;
        });
    }
    
    print_r($categorized);
    

    Output:

    Array
    (
        [A] => Array
            (
                [0] => Array
                    (
                        [category] => A
                        [Surname] => A Name 1
                    )
    
                [1] => Array
                    (
                        [category] => A
                        [Surname] => A Name 2
                    )
    
            )
    
        [B] => Array
            (
                [2] => Array
                    (
                        [category] => B
                        [Surname] => B Name 1
                    )
    
                [3] => Array
                    (
                        [category] => B
                        [Surname] => B Name 2
                    )
    
                [4] => Array
                    (
                        [category] => B
                        [Surname] => B Name 3
                    )
    
            )
    
        [C] => Array
            (
                [5] => Array
                    (
                        [category] => C
                        [Surname] => C Name
                    )
    
            )
    
        [D] => Array
            (
            )
    
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据