How to convert the null values to empty string in

2020-02-10 14:53发布

问题:

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

回答1:

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.



回答2:

PHP 5.3+

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


回答3:

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);


回答4:

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 :)



回答5:

You can use this instead.

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


回答6:

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']


回答7:

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


回答8:

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



回答9:

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.



标签: php arrays null