【问题标题】:Multidimensional array element if empty delete entire sub-array PHP多维数组元素如果为空则删除整个子数组 PHP
【发布时间】:2016-02-19 08:25:30
【问题描述】:

我正在尝试删除我的多维数组的子数组,如果其中任何一个值为空,则删除整个子数组。我想要一个通用的功能!不想键入特定的键。然后重新索引新形成的数组。

我的数组是这样的

Array
(
    [0] => Array
        (
            [name] => Test
            [mobile] => 613594551
            [email] => test@test.com
        )
    [1] => Array
        (
            [name] => Test1
            [mobile] => 613594552
            [email] => test2@test.com
        )
    [2] => Array
        (
            [name] => Test2
            [mobile] => 613594553
            [email] => test3@test.com
        )
    [3] => Array
        (
            [name] => Test3
            [mobile] => 613594554
            [email] => test4@test.com
        )
)

所以如果我的数组是

Array
(
    [0] => Array
        (
            [name] => 
            [mobile] => 613594551
            [email] => test@test.com
        )
    [1] => Array
        (
            [name] => Test1
            [mobile] => 
            [email] => test2@test.com
        )
    [2] => Array
        (
            [name] => Test2
            [mobile] => 613594553
            [email] => 
        )
    [3] => Array
        (
            [name] => Test3
            [mobile] => 613594554
            [email] => test4@test.com
        )
)

比显示

Array
(
    [0] => Array
        (
            [name] => Test3
            [mobile] => 613594554
            [email] => test4@test.com
        )
)

【问题讨论】:

  • 我想要一个通用的功能!现在我必须手动检查 if($array[$index]['name'])=="" 而不是 unset
  • @cronoklee 不完全是。仅当整个嵌套数组为空时,该函数才会从源数组中删除嵌套数组。这不是 OP 想要的。
  • @cronoklee 它不是重复的,那里的解决方案不适合我。

标签: php arrays multidimensional-array


【解决方案1】:

详细说明 Martin 的答案,您可以将 array_filter() 用于源数组和嵌套数组:

$filtered_array = array_filter($array, function($item){
    return count($item) == count(array_filter($item));
});
sort($filtered_array); // to reindex

工作示例:https://eval.in/521449

【讨论】:

    【解决方案2】:

    使用array_filter() 迭代每个人的所有记录并删除那些具有空值的记录。如果所有记录都已填充,array_filter() 之前和之后的记录数必须相等。如果他们没有删除使用 unset() 的人。

    请注意,此函数在原​​地工作,因此它会修改原始数组。

    <?php
    
    $array = [
        [
            'name' => 'Test1',
            'mobile' => 123456789,
            'email' => null
        ], [
            'name' => 'Test2',
            'mobile' => 123456789,
            'email' => 'test2@test.com'
        ], [
            'name' => null,
            'mobile' => 123456789,
            'email' => 'test3@test.com'
        ],
    ];
    
    function removeEmpty(&$arr) {
        foreach ($arr as $index => $person) {
            if (count($person) != count(array_filter($person, function($value) { return !!$value; }))) {
                unset($arr[$index]);
            }
        }
    }
    
    removeEmpty($array);
    
    print_r($array);
    

    打印:

    Array
    (
        [1] => Array
            (
                [name] => Test2
                [mobile] => 123456789
                [email] => test2@test.com
            )
    
    )
    

    【讨论】:

    • 这不会重新索引数组。
    【解决方案3】:

    假设一个数组项可能有不同的键:

        $array = array(
            0=>array('name'=>'','test'=>2),
            1=>array('name'=>'sadad','test'=>2),
            );
    
        foreach($array as $index=>$item) {
            $keys = array_keys($item); //here is the assumption
            //you can define it before the foreach
            //by checking the first array if YOU are 100% sure
            //all items in the array have the same keys: name, mobile, email
            foreach($keys as $key) {
                if(empty($item[$key])) {
                    unset($array[$index]);
                    break;
                }
            }
        }
    var_dump($array);
    

    输出:

    array(1) {
      [1]=>
      array(2) {
        ["name"]=>
        string(5) "sadad"
        ["test"]=>
        int(2)
      }
    }
    

    【讨论】:

    • 这也不会重新索引数组。
    • 添加$array = array_values($array);
    猜你喜欢
    • 1970-01-01
    • 2012-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-19
    • 2012-04-27
    相关资源
    最近更新 更多