很抱歉,我对这个问题进行了大量研究.是否有标准函数来搜索和替换数组元素?
I'm sorry but i researched a lot about this issue. Is there a standard function to search and replace array elements?
str_replace 在这种情况下不起作用,因为我想搜索的是一个空字符串 '' 而我想用空值替换它们
str_replace doesn't work in this case, because what i wanna search for is an empty string '' and i wanna replace them with NULL values
这是我的数组:
$array = (
'first' => '',
'second' => '',
);
我希望它变成:
$array = (
'first' => NULL,
'second' => NULL,
);
当然我可以创建一个函数来做到这一点,我想知道是否有一个标准函数来做到这一点,或者至少是一个单行解决方案".
Of course i can create a function to do that, I wanna know if there is one standard function to do that, or at least a "single-line solution".
我觉得没有这样的功能,所以我们新建一个
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 recursive
function map($value) {
if (is_array($value)) {
return array_map("map", $value);
}
return $value === "" ? NULL : $value;
};
$array3 = array_map("map", $array);
这篇关于用数组php中的空值替换空字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
mysql 中的 store_result() 和 get_result() 返回 falsestore_result() and get_result() in mysql returns false(mysql 中的 store_result() 和 get_result() 返回 false)
调用未定义的函数 mysqli_result::num_rows()Call to undefined function mysqli_result::num_rows()(调用未定义的函数 mysqli_result::num_rows())
PHP 准备好的语句问题PHP Prepared Statement Problems(PHP 准备好的语句问题)
mysqli_fetch_array 只返回一个结果mysqli_fetch_array returning only one result(mysqli_fetch_array 只返回一个结果)
PHP MySQLi 多次插入PHP MySQLi Multiple Inserts(PHP MySQLi 多次插入)
如何确保 MySQL 中的值在 PHP 中保持其类型?How do I make sure that values from MySQL keep their type in PHP?(如何确保 MySQL 中的值在 PHP 中保持其类型?)