在阵列PHP空更换空字符串 [英] Replacing empty string with nulls in array php

查看:129
本文介绍了在阵列PHP空更换空字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很抱歉,但我研究了很多关于这个问题。
是否有搜索和替换数组元素的一个标准功能?

I'm sorry but i researched a lot about this issue. Is there a standard function to search and replace array elements?

str_replace转换并不在这种情况下工作,因为我想搜索的是一个空字符串'',我想用NULL值替换它们。

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

这篇关于在阵列PHP空更换空字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆