如何将 php 数组中的 null 值转换为空字符串?

发布于 2024-10-15 06:53:04 字数 381 浏览 3 评论 0原文

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

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

(更改原因,与一般问题无关)

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

∞琼窗梦回ˉ 2024-10-22 06:53:04

PHP 5.3+

$array = array_map(function($v){
    return (is_null($v)) ? "" : $v;
},$array);

PHP 5.3+

$array = array_map(function($v){
    return (is_null($v)) ? "" : $v;
},$array);
吾家有女初长成 2024-10-22 06:53:04

然后,您应该循环遍历数组元素,检查每个值是否为 null 并将其替换为空字符串。类似的东西:

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

另外,您可以实现检查功能并使用 array_map()< /a> 函数。

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.

回忆追雨的时光 2024-10-22 06:53:04

还有更好/更短/更简单的解决方案。已给出答案的汇编。对于初始数据

[1, 4, "0", "V", null, false, true, 'true', "N"]

$result = array_map('strval', $arr);

$result 将是

<代码>['1', '4', '0', 'V', '', '', '1', 'true', 'N']

即使在 php4 中这也应该可以工作:)

There is even better/shorter/simpler solution. Compilation of already given answers. For initial data

[1, 4, "0", "V", null, false, true, 'true', "N"]

with

$result = array_map('strval', $arr);

$result will be

['1', '4', '0', 'V', '', '', '1', 'true', 'N']

This should work even in php4 :)

緦唸λ蓇 2024-10-22 06:53:04

这会将数组映射到一个新数组,该数组利用 null 三元运算符来包含数组的原始值,或者包含空字符串(如果该值为 null)。

$array = array_map(function($v){
    return $v ?: '';
},$array);

This will map the array to a new array that utilizes the null ternary operator to either include an original value of the array, or an empty string if that value is null.

$array = array_map(function($v){
    return $v ?: '';
},$array);
陪你搞怪i 2024-10-22 06:53:04

你可以用这个代替。

array_map(function($val){return is_null($val)? "" : $val;},$result);

You can use this instead.

array_map(function($val){return is_null($val)? "" : $val;},$result);
梦途 2024-10-22 06:53:04

这是我在上面的答案中没有提到的一种技术:

$val = strval(@$arr["notfound"]);  // will not generate errors and
                                   // defaults to an empty string

这对于 $_GET 参数加载非常方便,可以使内容简短易读。另外,您可以将 strval() 替换为 trim() ...或者如果您只接受整数,则可以替换为 intval()

如果缺少或非数字值,intval 的默认值为 0。如果为空、null 或 false,则 strval 的默认值为 ""

$val_str = strval(@$_GET['q']);
$val_int = intval(@$_GET['offset']);

查看 DEMO

现在对于数组,您仍然需要循环遍历每个值并设置它。但它非常可读,IMO:

$arr = Array (1, 4, "0", "V", null, false, true, 'true', "N");

foreach ($arr as $key=>$value) {
  $arr[$key] = strval($value);
}

echo ("['".implode("','", $arr)."']");

这是结果:

['1','4','0','V','','','1','true','N']

有趣的是,true 变成了“1”,但是 'true' 仍然是一个字符串,而 false 变成了空字符串 ""< /代码>。

现在使用 $arr[$key] = intval($value); 的相同数据会产生以下结果:

['1','4','0','0','0','0','1','0','0']

Here's a technique I haven't seen mentioned in the above answers:

$val = strval(@$arr["notfound"]);  // will not generate errors and
                                   // defaults to an empty string

This is super handy for $_GET parameter loading to keep things short and readable. Bonus, you can replace strval() with trim() ... or with intval() if you only accept integers.

The default for intval will be 0 if missing or a non-numeric value. The default for strval is "" if empty, null or false.

$val_str = strval(@$_GET['q']);
$val_int = intval(@$_GET['offset']);

See DEMO

Now for an array, you'll still need to loop over every value and set it. But it's very readable, IMO:

$arr = Array (1, 4, "0", "V", null, false, true, 'true', "N");

foreach ($arr as $key=>$value) {
  $arr[$key] = strval($value);
}

echo ("['".implode("','", $arr)."']");

Here is the result:

['1','4','0','V','','','1','true','N']

Interesting is that true becomes "1", but 'true' stays a string and that false becomes and empty string "".

Now the same data using $arr[$key] = intval($value); produces this result:

['1','4','0','0','0','0','1','0','0']
捂风挽笑 2024-10-22 06:53:04
foreach($array as $key=>$value)
{
if($value===NULL)
{
$array[$key]="";
}
}
foreach($array as $key=>$value)
{
if($value===NULL)
{
$array[$key]="";
}
}
红颜悴 2024-10-22 06:53:04

使用此功能。这会将嵌套数组中的 null 替换为空字符串,

$arr = array(
          "key1"=>"value1",
          "key2"=>null,
          "key3"=>array(
                 "subkey1"=>null, 
                 "subkey2"=>"subvalue2"),
          "key4"=>null);

    echo json_encode(replace_null_with_empty_string($arr));

   function replace_null_with_empty_string($array)
    {
        foreach ($array as $key => $value) 
        {
            if(is_array($value))
                $array[$key] = replace_null_with_empty_string($value);
            else
            {
                if (is_null($value))
                    $array[$key] = "";
            }
        }
        return $array;
    }

输出也将是:

{
  "key1": "value1",
  "key2": "",
  "key3": {
    "subkey1": "",
    "subkey2": "subvalue2"
  },
  "key4": ""
}

在此处在线尝试:https://3v4l.org/7uXTL< /a>

Use this function. This will replace null to empty string in nested array also

$arr = array(
          "key1"=>"value1",
          "key2"=>null,
          "key3"=>array(
                 "subkey1"=>null, 
                 "subkey2"=>"subvalue2"),
          "key4"=>null);

    echo json_encode(replace_null_with_empty_string($arr));

   function replace_null_with_empty_string($array)
    {
        foreach ($array as $key => $value) 
        {
            if(is_array($value))
                $array[$key] = replace_null_with_empty_string($value);
            else
            {
                if (is_null($value))
                    $array[$key] = "";
            }
        }
        return $array;
    }

Output will be :

{
  "key1": "value1",
  "key2": "",
  "key3": {
    "subkey1": "",
    "subkey2": "subvalue2"
  },
  "key4": ""
}

Try online here : https://3v4l.org/7uXTL

○闲身 2024-10-22 06:53:04

这可以使用一行解决:

array_walk($array_json_return,function(&$item){$item=strval($item);});

这里 $array_json_return 是数组。

This can be solved in one line using:

array_walk($array_json_return,function(&$item){$item=strval($item);});

here $array_json_return is the array.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文