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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?