1

Example array

$myArray[0] = array('23', null, '43', '12');
$myArray[1] = array(null, null, '53', '19');
$myArray[2] = array('12', '13', '14', null);

All nulls should be replaced with 0. I was hoping someone would have an efficient way of doing this, perhaps a built in PHP function that I am unaware of.

3
  • 3
    You know that the subarrays have only one element, namely a string? Commented Apr 15, 2010 at 18:27
  • There is no 'efficient' way of doing this - there's just one way, which is to traverse through all values in the array. It doesn't get any better than O(n) for this. Also, you made a typo in your code - get rid of those single quotes. Commented Apr 15, 2010 at 19:05
  • Related Replacing empty string with nulls in array php Commented Sep 18, 2024 at 20:45

3 Answers 3

18

You could use the array_walk_recursive function, with a callback function that would replace null by 0.


For example, considering your array is declared this way :
$myArray[0] = array(23, null, 43, 12);
$myArray[1] = array(null, null, 53, 19);
$myArray[2] = array(12, 13, 14, null);

Note : I supposed you made a typo, and your arrays are not containing only a string, but several sub-elements.


You could use this kind of code :
array_walk_recursive($myArray, 'replacer');
var_dump($myArray);

With the following callback functon :
function replacer(& $item, $key) {
    if ($item === null) {
        $item = 0;
    }
}

Note that :

  • the first parameter is passed by reference !
    • which means modifying it will modify the corresponding value in your array
  • I'm using the === operator for the comparison

And you'd get the following output :
array
  0 => 
    array
      0 => int 23
      1 => int 0
      2 => int 43
      3 => int 12
  1 => 
    array
      0 => int 0
      1 => int 0
      2 => int 53
      3 => int 19
  2 => 
    array
      0 => int 12
      1 => int 13
      2 => int 14
      3 => int 0
Sign up to request clarification or add additional context in comments.

2 Comments

@Felix : no, $item should not be an array : I'm using the recursive function ;-) which means the callback is called for the sub-elements (integers), and not the elements (arrays) themselves.
I noticed it seconds after my comments that is why i deleted my comment ;)
2

If the single quotation marks are unintentional, and the arrays have integers and null values:

for ($i = 0; $i < count($myArray); $i++)
{
    if ($myArray[$i] == null) $myArray[$i] = 0;
}

Comments

1

From PHP7.4 and higher, you can use arrow function syntax to make iterated calls of intval() on each row. This is safe to use on your data because none of the input values will be damaged by unconditionally casting them to int type data.

Code: (Demo)

var_export(
    array_map(fn($row) => array_map('intval', $row), $myArray)
);

Comments

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.