如何将空值转换为空字符串在PHP数组? [英] How to convert the null values to empty string in php array?

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

问题描述

我想这个数组转换的数组[4]不应该给空它可以给空格(空字符串)。

 阵列(
    [0] => 1
    [1] => 4
    [2] => 0
    [3] => V
    [4] =>
    [5] => ñ
);

(其中的原因的变化,无关的一般问题)

 致命错误:未捕获的异常
PDOException有消息数据库
错误[23000]:列'消息'不能
为空,驱动程序错误code是1048中


解决方案

然后,你应该只通过数组元素循环,检查各值空和空字符串替换它。这样的事情:

 的foreach($数组$关键=> $值){
    如果(is_null($值)){
         $阵列[$关键] =;
    }
}

此外,您还可以实现检查功能,并使用 array_map()函数

I want to convert this array that Array[4] should not give null it can give blank space (empty string).

Array (
    [0] => 1
    [1] => 4
    [2] => 0
    [3] => V
    [4] => 
    [5] => N 
);

(The reason for the change, unrelated to the general question)

Fatal error: Uncaught exception
'PDOException' with message 'Database
error [23000]: Column 'message' cannot
be null, driver error code is 1048' in

解决方案

Then you should just loop through array elements, check each value for null and replace it with empty string. Something like that:

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

Also, you can implement checking function and use array_map() function.

这篇关于如何将空值转换为空字符串在PHP数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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