Php 多维数组元素(如果为空)删除整个子数组

Php 多维数组元素(如果为空)删除整个子数组,php,arrays,multidimensional-array,Php,Arrays,Multidimensional Array,我试图删除多维数组的子数组,如果其中任何值为空,则删除整个子数组。我想要一个相同的通用函数!不想键入特定的键。然后对新形成的数组重新编制索引 我的数组就像 Array ( [0] => Array ( [name] => Test [mobile] => 613594551 [email] => test@test.com ) [1] => Arr

我试图删除多维数组的子数组,如果其中任何值为空,则删除整个子数组。我想要一个相同的通用函数!不想键入特定的键。然后对新形成的数组重新编制索引

我的数组就像

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
        )
)
用于迭代每个人的所有记录,并删除那些具有空值的记录。如果填写了所有记录,则
数组_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 = 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)
  }
}

详细说明Martin的答案,您可以对源数组和嵌套数组使用
array\u filter()

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

工作示例:

我想要一个相同的通用函数!现在我必须手动检查($array[$index]['name'])==“”是否与@cronoklee的副本不完全相同。仅当整个嵌套数组为空时,该函数才从源数组中删除嵌套数组。这不是OP想要的。@cronoklee它不是重复的,解决方案对我不起作用。这不会重新索引数组。这也不会重新索引数组。添加
$array=array\u值($array)
$filtered_array = array_filter($array, function($item){
    return count($item) == count(array_filter($item));
});
sort($filtered_array); // to reindex