P
P
Pavel2019-01-22 11:30:30
pfSense
Pavel, 2019-01-22 11:30:30

How to replace null with 0 in php in multidimensional array?

I have a multidimensional array. I want to replace null values ​​with 0. I'm trying to do this, but it doesn't work.

Написал такой код, но не получается.
 foreach($objWorksheet as $key => $val){
            if( $objWorksheet[$key] == null){
                $objWorksheet[$key] = 0;
            }
        }

Делаю дамп. 
dd($objWorksheet);

Выходит массив в таком же виде. 

array:4717 [▼
  0 => array:5 [▼
    0 => "Бренд"
    1 => "Артикул"
    2 => "Имя"
    3 => "Количество"
    4 => "Цена"
  ]
  1 => array:5 [▼
    0 => "AE"
    1 => "V94639"
    2 => null
    3 => 12.0
    4 => 581.0
  ]

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Eugene, 2016-05-23
@misant

Skype should be allowed past the proxy to those who can.

A
Alexey Ukolov, 2019-01-22
@sidiqi

Well, you have a multidimensional array, so $val is an array, it can never be null.

foreach ($objWorksheet as $index => $item) {
    foreach ($item as $key => $val) {
        if ($val === null){
            $objWorksheet[$index][$key] = 0;
        }
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question