Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert the null values to empty string in php array?

Tags:

arrays

php

null

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
like image 777
XMen Avatar asked Jan 28 '11 11:01

XMen


People also ask

Is null equal to empty string PHP?

is_null() The empty() function returns true if the value of a variable evaluates to false . This could mean the empty string, NULL , the integer 0 , or an array with no elements. On the other hand, is_null() will return true only if the variable has the value NULL .

What is null and empty in PHP?

A variable is NULL if it has no value, and points to nowhere in memory. empty() is more a literal meaning of empty, e.g. the string "" is empty, but is not NULL . The following things are considered to be empty: "" (an empty string) 0 (0 as an integer)

How do you declare an empty array in PHP?

Syntax to create an empty array: $emptyArray = []; $emptyArray = array(); $emptyArray = (array) null; While push an element to the array it can use $emptyArray[] = “first”. At this time, $emptyArray contains “first”, with this command and sending “first” to the array which is declared empty at starting.

Is null the same as 0 in PHP?

NULL essentially means a variable has no value assigned to it; false is a valid Boolean value, 0 is a valid integer value, and PHP has some fairly ugly conversions between 0 , "0" , "" , and false . Show activity on this post. Null is nothing, False is a bit, and 0 is (probably) 32 bits.


Video Answer


1 Answers

PHP 5.3+

$array = array_map(function($v){
    return (is_null($v)) ? "" : $v;
},$array);
like image 107
MythThrazz Avatar answered Oct 05 '22 02:10

MythThrazz