1

I have an Array that contains some entries. Some are string other are int. and those are defined in MySQL with type (int, varchar). The array i create looks like this: (minified the array because too long)

[3] => Array
    (
        [account_id] => *******
        [month_id] => 201903
        [month_comment] => 4,5% spend flat
        [month_spend] => 23000
        [month_budget] => 0
        [month_adops] => 1035
        [month_adops_forecast] => 1035
    )

[4] => Array
    (
        [account_id] => ******
        [month_id] => 201905
        [month_comment] => 
        [month_spend] => 
        [month_budget] => 0
        [month_adops] => 
        [month_adops_forecast] => 45
    )

[5] => Array
    (
        [account_id] => *******
        [month_id] => 201906
        [month_comment] => 
        [month_spend] => 
        [month_budget] => 0
        [month_adops] => 
        [month_adops_forecast] => 92
    )

As you can see some of "month_spend" and/or "month_adops" is empty, the problem is that in PHP the value is converted to "NULL" when the field is Integer in database, so result gives me:

Incorrect integer value: '' for column 'month_spend' at row 2"

So i tried to change this inside an foreach loop like this:

$result_replace = array();
foreach ($data_replace as $key => $result) {
    $result['month_id'] = substr_replace($result['month_id'], '2019', 0, 4);
    if(empty($result['month_budget'])) {
        $result['month_budget'] = 0;
        $result_replace[] = $result;
    }
    else if(empty($result['month_spend'])) {
        $result['month_spend'] = 0;
        $result_replace[] = $result;
    }
}

but looks like the foreach loop does not edit the data?

5
  • So in an array of multiple elements there can be empty "month_budget" or "month_spend" or "month_adops_forecast" and "month_adops" but i cannot use OR because some object does have value in two variables and other 3 variables. what is the best way to insert 0 if empty on these fields. Maby i should rewrite my question Commented Aug 23, 2018 at 11:28
  • 1
    Not sure if this helps, but you could update the data in place by adding & in $key => &$result. This will change the original array rather then having to create a replacement array. Commented Aug 23, 2018 at 11:29
  • 1
    Please show the SQL query Commented Aug 23, 2018 at 11:34
  • Why should PHP cast a 0 coming from the database to NULL? Never seen that before. Additionally, please edit your question to add more details, do not use the comment section for that Commented Aug 23, 2018 at 11:39
  • In your SQL, you could use IFNULL(month_budget, 0) Commented Aug 23, 2018 at 11:43

5 Answers 5

2

Use following to check both instead of else if

$result_replace = array();
foreach ($data_replace as $key => &$result) {
    $result['month_id'] = substr_replace($result['month_id'], '2019', 0, 4);
    if(empty($result['month_budget'])) {
        $result['month_budget'] = 0;
    }
    if(empty($result['month_spend'])) {
        $result['month_spend'] = 0;
    }
    $result_replace[] = $result;
}
Sign up to request clarification or add additional context in comments.

4 Comments

And if you want to also modify month_adops as mentioned in your question, just add an additional if :)
Worked like charm! i added more fields, is this a good practice?
Yes you can use if you have inconsistent data.
As your using &$result do you really need the $result_replace array?
0

Try replacing

empty()

by

is_null()

Comments

0

assumin your array is name $myArray

foreach ($myArray as $key => $result) {
  if(empty($result['month_budget'])) {
       $myArray[$key]['month_budget'] = 0
  }
  if(empty($result['month_spend'])) {
         $myArray[$key]['month_spend'] = 0
  }
}

var_dump($myArray)

in your case you are just assignt an empty array to $result_replace nothing else

Comments

0
  1. empty from 0 will return false. So your code will be redundant with this function. Try to use is_integer. If not integer assign zero. The bad part is that you cannot avoid the loop with foreach.

  2. is it possible to avoid the foreach from php by using a more concrete mysql query with e.g. IF(ISNULL(your_column), 0, your_column).

Comments

0
    array_walk_recursive($array, function (&$value, $key){
        if(!isset($value) && ('month_spend' === $key || 'month_adops' === $key)) {
            $value = 0;
        }
    });

1 Comment

This could potentially set empty string values (i.e. month_comment) to 0.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.