MySQL weird behavior with null integer - php

So I'm trying to implement php-etl to my application and MySQL doesnt let me insert null to nullable integer but it does if I manually change it, like this:
### This works ###
foreach($data as $row){
if($row["some_integer"] == Null){
$row["some_integer"] = Null;
}
if($row["some_other_integer"] == Null){
$row["some_other_integer"] = Null;
}
MyModel::create($row);
}
### This throws General error: 1366 Incorrect integer value ###
foreach($data as $row){
MyModel::create($row);
}
Tried both manually and with marquine/php-etl package. The input is csv file, null is empty space between ;; separators. Anybody knows what is going on here? Working on laravel 7.
Ok so the package loads the values as empty strings and by setting it Null it becomes real null... Is there a way to quicky convert these values to null?

MySQL won't treat an empty string as null, simply because an empty string and null are two different things. So the message you get from MySQL isn't weird, it's correct.
Nullify all empty strings in an array
If you just want to nullify all empty strings in an array, you can create a simple function for it:
function nullifyEmptyStrings(array $array)
{
return array_map(function ($item) {
return $item !== '' ? $item : null;
}, $array);
}
Then call it where ever/when ever you need it:
$row = nullifyEmptyStrings($row);
or use it directly in the argument for the create function:
MyModel::create(nullifyEmptyStrings($row));
Here's a demo
Nullify specific empty strings in an array
If you want to be able to define which array items to nullify, you can use this function:
function nullifyEmptyArrayValues(array $array, array $keys)
{
foreach ($keys as $key) {
if (array_key_exists($key, $array) && $array[$key] === '') {
$array[$key] = null;
}
}
return $array;
}
And in your code, you first pass the array containing the data and then another array with the keys to nullify, if the values are empty:
$row = nullifyEmptyArrayValues($row, ['some-key', 'another-key']);
Here's a demo

Related

Transform any null input to empty string

So I've got a while loop, inside I have $array_collections that gives me 35 value per loop, I want to verify for every value if it's equal to NULL then give it an empty string. I did that :
while ($array_collections = tep_db_fetch_array($query)) {
foreach ($array_collections as $key => $value) {
if (is_null($value)) {
$array_collections[$key] = "";
}
}
$docs[] = new \Elastica\Document('', \Glam\HttpUtils::jsonEncode(
array(
'X' => $array_collections['X'],
... etc
)));
}
This technically should work, but the loop goes over 500K elements so it's huge, and for every element we put it into a table, problem is that I run out of memory at some point. So is there another simple way to give any given NULL value an empty string without looping?
well, you can put NOT NULL constraint with empty string as DEFAULT value in the DB for that so you dont need to do that in php using looping, but if you dont want to change the DB design then you can use COALESCE in your query
select COALESCE(yourfield,'') from table
it will convert NULL value into empty string
You can use array_map function to replace null values into empty string.
$array_collections=array_map(function($ar)
{
if(isset($ar) && $ar!=null){
return $ar;
}
return '';
},$array_collections);
Above code replace all null values to empty string. No need of loop.
you can use array_walk:
function replace_null(&$lsValue, $key) {
if(is_null($lsValue)) {
$lsValue = "";
}
}
array_walk($array_collections, 'replace_null');

PHP Native "Count Set" Function

I have run into a point in my code where I would like to check if, within a dynamically set array, there are at least two set values before performing certain tasks. I cannot find a native PHP function which accomplishes this. Count returns all values, but does not allow me to check for the "truthiness" of the array values. Is there an equivalent native function for the following code:
function count_set($array = array()){
$count = 0;
foreach($array as $key => $value){
if($value){
$count++;
}
}
return $count;
}
Truthy scalar values are NOT false, 0, null, string 0 or an empty string (see Converting to boolean). array_filter() will remove these by default if you don't provide a callback:
$count = count(array_filter($array));

Prevent json_encode to encode empty strings as null

Is it possible for the PHP json_encode function do not convert empty string values to null?
UPDATE
I can not replicate this behaviour in clear conditions, and looks like it's already a default for this function.
This is already the default behavior.
json_encode(['test' => '']);
generates:
{"test":""}
No that i know of, but you could do this:
array_walk_recursive($value, function (&$item, $key) {
$item = null === $item ? '' : $item;
});

PHP: checking key value is not empty

I wrote a small function to check the required fields of a form, are not empty.
The function accepts two arguments, 1st is an array with all values from $_POST superglobal.
2nd is the required fields array which I populate.
Have a look:
public $errors = array();
public function validate_fields($fields_array, $required_fields)
{
foreach ($required_fields as $key => $value)
{
if (array_key_exists($key, $fields_array))
{
# If key exists in $fields_array
# check that the key value inside $fields_array is set & isn't empty
# if it's empty, populate with an error
if(empty($fields_array[$key][$value]))
{
$this->errors[] = "{$key} is empty but in fields_array";
}
}
else
{
# Key does not exists in $fields_array
# Did someone temper with my html ?
$this->errors[] = "{$key} is not in fields_array";
}
}
return (empty($this->errors)) ? true : false;
}
The issue I'm having seems to be related to "if(empty($fields_array[$key][$value]))"
statement. my goal is to check that $fields_array key value is not empty based on $required_fields key. I'm sure the statement I'm using is off. If you see anything that you think can be written better, please let me know, as I am new to php. Appreciate the help.
I think what you're trying to do is:
if(empty($fields_array[$key])) {
//this means value does not exist or is FALSE
}
If you also want to check for empty-strings or white-space only, then you need something more than just empty. E.g.
if(empty($fields_array[$key]) || !trim($fields_array[$key])) {
//this means key exists but value is null or empty string or whitespace only
}
Do note that the above answers will only work for indexed arrays in >PHP 5.4. If you have an associative array you have to use isset instead of empty:
if(isset($fields_array[$key]) && trim($fields_array[$key]) != '')
See http://nl3.php.net/manual/en/function.empty.php, example #2
You don't need to select the value as an index just key. Where $fields_array[$key] = $value;
if(empty($fields_array[$key]) && trim($fields_array[$key]) != '')

PHP array searching question

I have arrays, some are multidimensional as well. I need a way to know if all the values are NULL. What would be the most efficient and effective way of determining if ALL values of an array (as well as the values of the arrays within that array) are NULL?
So basically: search array, if all values are NULL, $flag = true
EDIT: The values are being pulled from a MySQL database where NULL is in the context of MySQL, if that makes a difference.
If i remember well db null fields become empty strings in php so you can use a function like this:
function isEmptyArray($array){
foreach($array as $val)
if((is_array($val) && !isEmptyArray($val))||(!is_array($val) && $val!="")) return false;
return true;
}
Depends on what would you use that flag for, but I think the best would be to query the database so it retrieves only non null values, like
select col1,col2,col3 from table where col1 is not null and col2 is not null
and col3 is not null
That said, a simple way to do something like that in PHP would be
//array_filter filters values that evaulate to fals
$result = array_filter($input);
if (count($result) == 0) { $flag = true; }
This will fail for multidimensional arrays and if zeroes or other values that get automatically converted to false are valid values in your array, if this is the case you have to build a recursive callback function and pass it as a second parameter to array_filter.
You may want to look at the array_filter() function, by default it strips out values that equate to false (this includes NULL).
I'm not sure if it'll work as is for mutli-dimensional arrays, but it has the option to use call back functions which would easily allow you to handle that.
Long story short, if you run array_filter on the array and you get an empty array returned then all your values are Null or equal to false - if you need to differentiate between these values in a strict sense, then you should use a call back.
Can be done recursively:
arrayIsFullyNull ($arr)
{
if (is_array($arr))
foreach ($arr as $val)
if (!arrayIsFullyNull($val))
return false;
else if (!is_null($arr))
return false;
else
return false; // it's not null, and not array => value
return true;
}
You could use a RecursiveArrayIterator to walk over all the values (leaves of the tree of of nested arrays).
Untested code:
function isNullOrNestedArrayOfNulls($array)
{
if (is_null($array)
{
return true;
}
else if (!is_array($array))
{
return false;
}
$allNull = true;
$iter = new RecursiveIteratorIterator(new RecursiveArrayIterator($array),
RecursiveIteratorIterator::LEAVES_ONLY);
while ($iter->valid())
{
if (!is_null($iter->current())
{
$allNull = false;
break;
}
}
return $allNull;
}
This would have the advantage of avoiding a recursive function (which have the reputation of being rather inefficient in PHP).

Categories