Replacing empty string with nulls in array php Replacing empty string with nulls in array php arrays arrays

Replacing empty string with nulls in array php


I don't think there's such a function, so let's create a new one

$array = array(   'first' => '',   'second' => '');$array2 = array_map(function($value) {   return $value === "" ? NULL : $value;}, $array); // array_map should walk through $array// or recursivefunction map($value) {   if (is_array($value)) {       return array_map("map", $value);   }    return $value === "" ? NULL : $value;};$array3 = array_map("map", $array);


As far as I know, there is no standard function for that, but you could do something like:

foreach ($array as $i => $value) {    if ($value === "") $array[$i] = null;}


Now you can use arrow function also:

$array = array_map(fn($v) => $v === '' ? null : $v, $array);