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
Here's a technique I haven't seen mentioned in the above answers:
This is super handy for
$_GET
parameter loading to keep things short and readable. Bonus, you can replacestrval()
withtrim()
... or withintval()
if you only accept integers.The default for
intval
will be0
if missing or a non-numeric value. The default forstrval
is""
if empty, null or false.See DEMO
Now for an array, you'll still need to loop over every value and set it. But it's very readable, IMO:
Here is the result:
Interesting is that
true
becomes "1", but'true'
stays a string and thatfalse
becomes and empty string""
.Now the same data using
$arr[$key] = intval($value);
produces this result:This can be solved in one line using:
here $array_json_return is the array.
Then you should just loop through array elements, check each value for null and replace it with empty string. Something like that:
Also, you can implement checking function and use array_map() function.