Answer the question
In order to leave comments, you need to log in
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
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 questionAsk a Question
731 491 924 answers to any question