Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

knubbe82's avatar

How to remove arrays with empty values?

How to remove arrays with empty values?

array:21 [▼
  0 => array:17 [▶]
  1 => array:17 [▶]
  2 => array:17 [▶]
  3 => array:17 [▶]
  4 => array:17 [▶]
  5 => array:17 [▶]
  6 => array:17 [▶]
  7 => array:17 [▶]
  8 => array:17 [▶]
  9 => array:17 [▶]
  10 => array:17 [▶]
  11 => array:17 [▶]
  12 => array:17 [▶]
  13 => array:17 [▶]
  14 => array:17 [▶]
  15 => array:17 [▶]
  16 => array:17 [▶]
  17 => array:17 [▶]
  18 => array:17 [▶]
  19 => array:17 [▶]
  20 => array:17 [▼
    "rbr" => null
    "kupac" => null
    "sifra_sap" => null
    "pan" => null
    "benzinska_stanica" => null
    "datum_transakcije" => null
    "tociono_mesto" => null
    "nosilac_kartice" => null
    "broj_racuna" => null
    "kilometraza" => null
    "sipano_van_rezervoara" => null
    "naziv_proizvoda" => null
    "kol" => null
    "cena" => null
    "total" => null
    "valuta" => null
    "" => null
  ]
]

For example I don't want this [20] array at all

0 likes
20 replies
mstrauss's avatar

@knubbe82

Laravel's Collection methods are really useful for cases. Something like this should work.

$collection = collect($yourArray); //first convert the plain array to a Collection

$filtered = $collection->reject(function ($value, $key) {
    return count($value) === 0;
});
Sinnbeck's avatar

Do you want to remove the empty inner keys if they are empty. For instance if only some are null, should they be removed?

knubbe82's avatar

remove complete array where all values are null. Just like I said in question: array with index 20 must be removed

Jaytee's avatar

Here's something I quickly whipped up on the command line using PHP functions. I'd suggest that you convert the array into a collection and use the methods that collections have available to them.

$data = [
    [
        'id' => 1,
        'name' => 'Alpha'
    ],
    [
        'id' => 2,
        'name' => 'Bravo'
    ],
    [
        'id' => null,
        'name' => null
    ]
];

foreach ($data as $index => $arr) {
    $result = array_filter($arr, function ($value) {
        return !is_null($value);
    });

    if (count($result) == 0) {
        unset($data[$index]);
    }
}

var_dump($data);

Result

array(2) {
  [0]=>
  array(2) {
    ["id"]=>
    int(1)
    ["name"]=>
    string(5) "Alpha"
  }
  [1]=>
  array(2) {
    ["id"]=>
    int(2)
    ["name"]=>
    string(5) "Bravo"
  }
}
bugsysha's avatar
bugsysha
Best Answer
Level 61

Is this the solution?

collect($items)->reject(function (array $item) {
    return count($item) === count(array_filter($item, 'is_null'));
});

Or maybe you can omit count() calls like this

collect($items)->reject(function (array $item) {
    return $item === array_filter($item, 'is_null');
});
1 like
Sinnbeck's avatar

Great. I would do it like this

$filtered = collect($array)->reject(function ($value) {
    return collect($value)->reject(function ($value) {
    return !$value
})->isEmpty();
});
artcore's avatar

I also use array_filter to remove null values. Be aware that the array keys are removed as well so you may also need to run it through array_merge() to fix the indexes. JavaScript for instance would see it as an object and no longer as array

Snapey's avatar

Rather than test every value in each item, is there a required field that you can test so that if that is null then it does not matter if the other values are set (or its impossible that they will be).

bugsysha's avatar

@knubbe82 you would've been better to start working with me than you've decided to stay at your current job :P Also tell Max to hire me as a consultant :D

It is nice to see you here @knubbe82 Best regards.

1 like
Sinnbeck's avatar

@knubbe82 Be aware that the best answer will not work if one of the values is an empty sting instead of null. Just in case that could happen :) If not, its a very good and clean solution :)

bugsysha's avatar

@sinnbeck but he did not ask for a solution where one of the values is an empty string. There are many solutions. This is one for his example that he asked for.

Sinnbeck's avatar

@bugsysha True. And your solution is elegant and the best answer. I'm simply trying to be nice and give him a heads up if that could end up being an issue 😊 "remove empty values" could mean different things

bugsysha's avatar

@sinnbeck I think that he just needs a general guideline and not specific solution. Most of the users here just present simplified problems in which way they try to protect the data they are working with or something like that.

If we have to think about all possibilities that might occur, our heads would grow exponentially and they would have to give us a chunk of their salary :D

1 like
bugsysha's avatar

@knubbe82 recommend me anyway :D

What are you doing now? Found something better where leads are better?

Please or to participate in this conversation.