Asim
Asim

Reputation: 976

NULL to 0 - PHP array

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?

Upvotes: 1

Views: 805

Answers (5)

TsV
TsV

Reputation: 639

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

Upvotes: 0

Badarau Petru
Badarau Petru

Reputation: 59

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

Upvotes: 0

ScaisEdge
ScaisEdge

Reputation: 133390

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

Upvotes: 0

BloodyMonkey
BloodyMonkey

Reputation: 1634

Try replacing

empty()

by

is_null()

Upvotes: 0

Jasmin Mistry
Jasmin Mistry

Reputation: 1476

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

Upvotes: 2

Related Questions