我想递归地用空值或空字符串替换数组中的所有空值。
目前,我的数组是这样的,这在结构上是完美的,我想用空字符串替换它中的每个null值。
Array
(
[items] => Array
(
[0] => Array
(
[id] => 28
[name] => ABC
[goal] =>
[currency] =>
[images] =>
[start] => 1446159600
[stop] => 1446246000
)
[1] => Array
(
[id] => 29
[name] => XYZ
[goal] =>
[currency] =>
[images] =>
[start] => 1446159600
[stop] => 1446246000
)
)
)
请告诉我更换它的最短方法。
正如Rizier123所指出的,您可以使用array_walk_recurative 来实现这一点
function replaceNullValueWithEmptyString(&$value) {
$value = $value === null ? "" : $value;
}
array_walk_recursive($array, "replaceNullValueWithEmptyString");
如果使用array_map(),则可以获得结果数组函数。这是示例代码。
$item = array('a' => 'apple', 'b' => 'banana','c' => 'rama', 'd' => 'lingam','e' => '', 'f' => '');
function addNull($n)
{
if($n=='')
return('NULL');
else
return($n);
}
$result_array = array_map("addNull", $item);
print_r($result_array);