I have a use case whereby an array must be created that contains the key/value pairs that have an empty value in the original array.
This sample data is a simplified representation of the actual data. The code below results in the desired outcome but is there a more concise way to write this? Is there an alternative to using a foreach
that's shorter than the code below?
$data = ['beans' => 23, 'peas' => null, 'carrots' => 17, 'turnips' => '', 'potatoes' => 58];
$result = [];
foreach ($data as $key => $value) {
if (empty($value)) $result[$key] = $value;
}
echo print_r($result, 1);