多维数组元素(如果为空)删除整个子数组PHP [英] Multidimensional array element if empty delete entire sub-array PHP

查看:116
本文介绍了多维数组元素(如果为空)删除整个子数组PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to delete sub array of my multidimensional array, if any of the value is empty, than delete entire sub array. I want a universal function for the same! Dont want to type specific keys. And than ReIndex the newly formed array.

我的数组就像

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
        )
)

推荐答案

详细说明马丁的答案,您可以对源数组和嵌套数组都使用 array_filter():

Elaborating on Martin's answer, you can use array_filter() for both the source array and the nested array:

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

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

这篇关于多维数组元素(如果为空)删除整个子数组PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆