1

I have an array which looks like this:

array:6 [▼
    0 => 2
    1 => 2
    2 => 2
    6 => 5
    10 => 3
    11 => 1
]

I would like to check for a range of numbers e.g. 0 to 11 if these keys exist in my array. If not I want to create the element with the missing key and give it the value 0.

So I would end up with an array like this:

array:6 [▼
   0 => 2
   1 => 2
   2 => 2
   3 => 0
   4 => 0
   5 => 0
   6 => 5
   7 => 0
   8 => 0
   9 => 0
   10 => 3
   11 => 1
]

I tried something like this:

$range = range(0,11);

foreach($myArray as $key => $value){
  if(!in_array($key, $range)){
    $myArray[$key] = 0;
  }
}

But I just get the same array as at the beginning of the question.

2
  • 1
    And where are you stuck at doing this? Commented May 10, 2016 at 11:16
  • Do you need to fill the missing array keys to with value 0?? Commented May 10, 2016 at 11:17

5 Answers 5

7

You can create an array with array_fill_keys() and pass it the amount of keys you want with range() and fill the array with 0's. After that you can replace all elements which you already have in your array with array_replace().

<?php

    $array = [2 => 3, 5 => 2, 11 => 7];

    $result = array_replace(array_fill_keys(range(0, 11), 0), $array);
    print_r($result);

?>

The problem with your code is that you only loop over the elements of your array. So if you have just 3 elements you will just loop over these 3 elements.

If you want to fix your current code you would have to loop over the $range and then check if the key, not the value, exists in your array and if yes use the value from it otherwise create the element with the value 0.

Fixed code:

$range = range(0,11);
$result = [];

foreach($range as $key){
    if(isset($array[$key]))
        $result[$key] = $array[$key];
    else
        $result[$key] = 0;
}
Sign up to request clarification or add additional context in comments.

Comments

1

You can try following code if you have range of array key upto 11 only.

for($i=0; $i<=11;$i++){
    if(!array_key_exists($i,$array)){
      $array[$i] = 0;
    }
}

3 Comments

use max(array_keys($array)) instead 11 it will find max key in the array; and you don't need to check last item (11) because it always will be exists.
Using your method it will append all 0 keys at end.
try if(!isset($array[$i])) instead of if(!array_key_exists($i,$array))
0

Use below code.

1) Using key() we find last key number.

2) Using array_key_exists(key,array) check for key exist in array or not.

3) Fill it in new array.

$arr = array(
    0 => 2,
    1 => 2,
    2 => 2,
    6 => 5,
    10 => 3,
    11 => 1,
   );
end($arr);
$endKey = key($arr);

for($i=0;$i<$endKey;$i++)
{
    if(!array_key_exists($i, $arr))
    {
        $newarr[$i] = 0;
    }
    else
    {
        $newarr[$i] = $arr[$i];
    }
}

Output

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

1 Comment

why minus vote?. Its working i have checked and tested. @Marco
0

The solution using array_diff_key and array_fill functions:

// $arr is your initial array
$diff = array_diff_key(array_fill(0,12,0), $arr);
$result = $arr + $diff;
ksort($result);

print_r($result);

The output:

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

Comments

-1

You need to browse the whole array for set missing keys value 0. I make a function for doing this, Check if the index is missing then set 0.

$array = array('0' => 2, '1' => 2, '2' => 2, '6' => 5, '10' => 3, '11' => 1);
function cleanArray(&$array){
    end($array);
    $max = key($array); //Get the final key as max!
    for($i = 0; $i < $max; $i++){
        if(!isset($array[$i])){
            $array[$i] = 0;
        }
    }
}

cleanArray($array);
ksort($array);
print_r($array);

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.