dpzbzp8728 2016-05-05 17:07
浏览 66
已采纳

关于“简单”多级数组的PHP Array_filter

Can I and how can I use PHP's array_filter to filter the blank/nulls entries out of the following array structure?

From: The array is from a PDO call using Fetch BOTH so the numeric and named values are always equal in this case.

Array
(
    [2400] => Array
        (
            [0] => Array
                (
                    [value] => 7
                    [0] => 7
                )
            [1] => Array
                (
                    [value] => 61
                    [0] => 61
                )
            [2] => Array
                (
                    [value] => 42
                    [0] => 42
                )
            [3] => Array
                (
                    [value] => 
                    [0] => 
                )
        )
)

To:

Array
(
    [2400] => Array
        (
            [0] => Array
                (
                    [value] => 7
                    [0] => 7
                )
            [1] => Array
                (
                    [value] => 61
                    [0] => 61
                )
            [2] => Array
                (
                    [value] => 42
                    [0] => 42
                )
        )
)

I have tried

  • plain old array_filter
  • array_filter(array, function($f){ ??? }) and not quite sure where to go from here... I was going to foreach the array to delve into it but how will that affect the entries through array_filter? Won't a true/false return bring in the entire [2400] array portion? It just has me confused.

Please suggest improvements to the question

  • 写回答

3条回答 默认 最新

  • dongnius85154 2016-05-06 06:10
    关注

    I think this can not be done using only array_filter function because sometimes you need to modify array elements but the array_filter function allows only to decide if the element should be excluded or not.

    For example in the main array element with index 2400 should be included in the result set but it's content should be modified.

    I wrote a simple function to do this, hope it might help. Well, you might use this for inspiration. And it was interesting challenge for me as well.

    Below is my function with couple tests.

    <?php
    
    function deepFilter(array $array)
    {
        // Formally this is not need because if array is empty then $filteredArray will also be empty
        // but it simplifies the algorithm
        if (empty($array)) {
            return [];
        }
    
        $filteredArray = [];
        foreach ($array as $key => $value) {
            if (is_array($value) && !empty($value)) {
                $value = deepFilter($value);
            }
            if (!empty($value)) {
                $filteredArray[$key] = $value;
            }
        }
    
        return $filteredArray;
    }
    
    $testArray1 = [
        2400 => [
            0 => [
                'value' => 7,
                0 => 7,
            ],
            1 => [
                'value' => 61,
                0 => 61,
            ],
            2 => [
                'value' => 42,
                0 => 42,
            ],
            3 => [
                'value' => null,
                0 => null,
            ]
        ]
    ];
    
    $testArray2 = [
        2400 => [
            0 => [
                'value' => 7,
                0 => 7,
            ],
            1 => [
                'value' => 61,
                0 => 61,
            ],
            2 => [
                'value' => 42,
                0 => 42,
            ],
            3 => null
        ],
        3243 => [
            0 => [
                'value' => 7,
                0 => null,
            ],
            1 => [
                'value' => null,
                0 => 61,
            ],
            2 => [
                'value' => 42,
                0 => 42,
            ],
            3 => null
        ]
    ];
    var_export(deepFilter($testArray1));
    var_export(deepFilter($testArray2));
    

    The idea is very simple.

    1. Take an array and check elements one by one.
    2. If element is an array, apply the function for that element and check the result. We can remove everything from child array and in this case we should not add it to results. Else if child has something remaining after cleanup include 'cleaned child' in our result set.
    3. If our element is not an array then include it only if it's not empty.

    Please let me know if you find any mistakes or if it works for you or not.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 滑块验证码移动速度不一致问题
  • ¥15 定制ai直播实时换脸软件
  • ¥100 栈回溯相关,模块加载后KiExceptionDispatch无法正常回溯了
  • ¥15 麒麟V10桌面版SP1如何配置bonding
  • ¥15 Marscode IDE 如何预览新建的 HTML 文件
  • ¥15 K8S部署二进制集群过程中calico一直报错
  • ¥15 java python或者任何一种编程语言复刻一个网页
  • ¥20 如何通过代码传输视频到亚马逊平台
  • ¥15 php查询mysql数据库并显示至下拉列表中
  • ¥15 freertos下使用外部中断失效