0

$firstbloodkill array:

Array ( [0] => [1] => [2] => [3] => [4] => [5] => 1 [6] => [7] => [8] => [9] => )

I'm trying to set null values to 0.

What im trying to make:

Array ( [0] => 0 [1] => 0 [2] => 0 [3] => 0 [4] => 0 [5] => 1 [6] => 0 [7] => 0 [8] => 0 [9] => 0 )

My code:

   foreach ($firstbloodkill as $key => $element)
    {
        if($element == "")
        {
            $firstbloodkill[$key] = 0;
        }

    }

Its not working. What i am doing wrong?

Edit: I found what i am doing wrong. Arrays name was 'firstBloodKill'. I changed firstbloodkill to firstBloodKill and it started working. Sorry im dumb.

1
  • Check what exactly you have as value: var_dump($element); Commented Jun 10, 2016 at 10:52

3 Answers 3

1
 foreach($firstbloodkill as $key => $element) {
     if(empty($element)) {
         $firstbloodkill[$key] = 0;
     }
 }
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you its working ill mark it as correct answer. My code was working too. Im dumb arrays name was firstBloodKill and i typed firstbloodkill. thats why it was not working.
1

You can simply use empty function like as

foreach($arr as &$v){
    if(empty($v)){
        $v = 0;
    }
}

Comments

1

Hi You can also use array_map for this

<?php 
function change($value){
    if(empty($value))return 0;
    else return $value;
}
$firstbloodkill = array(0 =>'',1 =>'' ,2 =>1,3 =>'' ,4 =>'' ,5 =>1 ,6 =>'' ,7 =>'' ,8 =>'' ,9=>'' );
$new = array_map('change',$firstbloodkill);
echo "<pre>";print_r($new);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.