Fill fields of a multidimensional array with null

Asked

Viewed 1,688 times

2

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

That’s how it works, but for my array multidimensional 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 @Jader’s solution, the array_walk_recursive, Maybe I applied the same solution of @Papa Charlie but I ended up understanding better by the reply of @Jader. The solution presented allowed me with the function to access several levels of array that was multidimensional.

  • An empty Dice is null.

  • I edited the title to make it more suggestive, okay?

4 answers

5


To get recursiveness on "infinite" levels, you need to create a function, and this function must verify that the value is an array and apply itself, thus:

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 cannot be done with the 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 simply change the function array_walk for array_walk_recursive, gets the same effect:

array_walk_recursive($array , function( &$value , $field){
    if(! $value) $value = '0';
});
  • Your example passes the filter field as an array, put a rule for empty array to return zero, so it will only have string in the result and can play in direct DB as it intends.

4

array_walk - Apply a certain 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 all value null by 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
)
  • I have a array within another array and it made the code you gave me not work. Beyond this situation of array multidimensional, still has it: $res = $client->get($array) which is probably hindering the execution of the code you gave me.

  • I’ll adjust the code

  • Recalling that I need to apply the code in this structure that is the structure of the web-service: http://pastebin.com/PaVh3cMm

  • updated response

  • 2

    @Papacharlie So you have no recursion, only the first level will be processed, and still generates error if the value is not array... See array_walk_recursive.

  • yes yes, this incomplete

  • http://axitech.com.br/vista/alimentador.xml ... @Papacharlie solved a lot of my problems, removed the CDATA and added 0 empty fields to avoid error in the automation of XML who actually made a mistake because of the empty fields.

  • Made a mistake error on line 594

  • I try and when I greatly increase the number of records, it gives error. Up to 50 records it goes well, then it doesn’t work anymore. But because he goes straight up to a certain point and then plants?

  • now you seem to be right

Show 5 more comments

2

Simply iterate on the elements and change their value if they are null:

foreach($res as $key => $val) {
  if (empty($res[$key])) $res[$key] = null;
}
  • Actually I can’t remove them, I really have to insert null in empty fields because this file will be part of a dump later, then I need all fields that will be columns in the database to be intact.

  • Sorry, man, I got it wrong. Answer corrected! =)

  • 2

    But still you could improve your question, with smaller examples for input and output code.

2

Buddy, 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
    }
}
  • The model of my code is a array within another array. Seeing the code can give a better solution to my case? And you @Peoplee?

  • 1

    I’ll check here

Browser other questions tagged

You are not signed in. Login or sign up in order to post.