Anita Mourya
Anita Mourya

Reputation: 115

Multidimensional array element if empty delete entire sub-array 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.

My array is like

Array
(
    [0] => Array
        (
            [name] => Test
            [mobile] => 613594551
            [email] => [email protected]
        )
    [1] => Array
        (
            [name] => Test1
            [mobile] => 613594552
            [email] => [email protected]
        )
    [2] => Array
        (
            [name] => Test2
            [mobile] => 613594553
            [email] => [email protected]
        )
    [3] => Array
        (
            [name] => Test3
            [mobile] => 613594554
            [email] => [email protected]
        )
)

So if my array is

Array
(
    [0] => Array
        (
            [name] => 
            [mobile] => 613594551
            [email] => [email protected]
        )
    [1] => Array
        (
            [name] => Test1
            [mobile] => 
            [email] => [email protected]
        )
    [2] => Array
        (
            [name] => Test2
            [mobile] => 613594553
            [email] => 
        )
    [3] => Array
        (
            [name] => Test3
            [mobile] => 613594554
            [email] => [email protected]
        )
)

Than display

Array
(
    [0] => Array
        (
            [name] => Test3
            [mobile] => 613594554
            [email] => [email protected]
        )
)

Upvotes: 2

Views: 1644

Answers (3)

Sergey Vidusov
Sergey Vidusov

Reputation: 1342

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

Working example: https://eval.in/521449

Upvotes: 2

ka_lin
ka_lin

Reputation: 9432

Assuming that an array item might have different keys:

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

Output:

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

Upvotes: 0

martin
martin

Reputation: 96899

Use array_filter() to iterate all records for each person and remove those that have an empty value. Number of records before and after array_filter() has to be equal if all records are filled. If they're not remove the person using unset().

Note, that this function works in-place, so it modifies the original array.

<?php

$array = [
    [
        'name' => 'Test1',
        'mobile' => 123456789,
        'email' => null
    ], [
        'name' => 'Test2',
        'mobile' => 123456789,
        'email' => '[email protected]'
    ], [
        'name' => null,
        'mobile' => 123456789,
        'email' => '[email protected]'
    ],
];

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

Prints:

Array
(
    [1] => Array
        (
            [name] => Test2
            [mobile] => 123456789
            [email] => [email protected]
        )

)

Upvotes: 0

Related Questions