Fill fields in a multidimensional array with null

2

I have this code that returns this array and if you notice, you can see that there are empty fields. What can I increment in this code so that where the index is empty receive the value null ?

This works, but for my multidimensional array it did not work.

$array = array('A'=>1,'B'=>'','C'=>3,'D'=>'','E'=>5,'F'=>6);

array_walk($array , function( &$value , $field){
    if(! $value) $value = '0';
});

print_r($array);

I resolved my application using the @Jader solution, array_walk_recursive , maybe applied the same @Papa Charlie solution but I ended up getting a better understanding of @Jader's response. The solution presented allowed me with multi-level access to the array function that was multidimensional.     

asked by anonymous 07.09.2014 / 02:45

4 answers

5

To get recursion at "infinite" levels, you need to create a function, and this function should check if the value is an array and apply itself, like this:

Note: I keep this form in the answer because it can be useful for other people if the purpose of the function is more complex and can not be done with array_walk_recursive .

function null_array($array) {
    foreach($array as &$value) {
        if (is_array($value)) $value = null_array($value);
        else if (empty($value)) $value = '0';
    }
    return $array;
}

$array = array('A'=>1,'B'=>'','C'=>3,'D'=>'','E'=>5,'F'=>6, 'G' => array('A'=>1,'B'=>'','C'=>3,'D'=>'','E'=>5));

$array = null_array($array);

print_r($array);

// retorno
Array
(
    [A] => 1
    [B] => 0
    [C] => 3
    [D] => 0
    [E] => 5
    [F] => 6
    [G] => Array
        (
            [A] => 1
            [B] => 0
            [C] => 3
            [D] => 0
            [E] => 5
        )

)

Or just change the array_walk function to array_walk_recursive , you get the same effect:

array_walk_recursive($array , function( &$value , $field){
    if(! $value) $value = '0';
});
    
07.09.2014 / 03:55
4
  

array_walk - Applies a specific function to each element of an array

array_walk( $arrays , function( &$array ){
    foreach( $array as $item => &$value ) {
        if( ! $value ) $value = '0';
    }
});

print_r( $arrays );

This will replace every value null with zero

output:

Array(
    [0] => Array
            [Data cadastro] => 2012-03-16
            [Endereco] => CASEMIRO DE ABREU
            [Data Ativacao] => 2014-02-10
    [1] => Array
            [Data cadastro] => 2012-03-16
            [Endereco] => CASEMIRO DE ABREU
            [Data Ativacao] => 0
)
    
07.09.2014 / 03:39
2

Just iterate through the elements and change their value if they are null:

foreach($res as $key => $val) {
  if (empty($res[$key])) $res[$key] = null;
}
    
07.09.2014 / 02:53
2

Friend, I think this is what you're looking for:

$minhaArray = array('A'=>1,'B'=>'','C'=>3,'D'=>'','E'=>5,'F'=>6);

foreach($minhaArray as $elemento) {
    if(strlen($elemento) == 0) {
        unset($elemento); // Aqui faz o elemento vazio ficar NULL
    }
}
    
07.09.2014 / 03:11