Include numbers to complete numeric sequence in PHP

Asked

Viewed 309 times

0

In this case I have the result in MYSQL.

01
03
06
09

I need to complete with the number correct sequence up to digit 12.

Example:

01
02
03
04
05
06
07
08
09
10
11
12
  • 3

    @Andersoncarloswoss this is to "include" the numbers. But yes, it could already be dealt with directly in the loop. rs

2 answers

1


I do not see well the need to complete, following the current need that sounds, it seems that only needs 1 to 12, so the use of range with str_pad would already solve:

$valores = range(1, 12);

$valores = array_map(function ($item) {
    return str_pad($item, 2, '0', STR_PAD_LEFT);
}, $valores);

var_dump($valores);

Something that I think maybe would make sense would be you research the missing numbers and return them:

function array_missing(array $arr, $min = 12)
{
    $max = max($arr);

    //Se no array tiver um valor maior que o $min
    if ($max > $min) {
       $min = $max;
    }

    $values = range(1, $min);

    $values = array_map(function ($item) {
        return str_pad($item, 2, '0', STR_PAD_LEFT);
    }, $values);

    return array_diff($values, $arr); 
}

$exemplo = array( '01', '03', '06', '09');

var_dump(array_missing($exemplo));

Note: the $min = 12 is to set the minimum to be generated, if the array has a higher value then it will generate more items.

This way it will return the missing:

array(8) {
  [1]=>
  string(2) "02"
  [3]=>
  string(2) "04"
  [4]=>
  string(2) "05"
  [6]=>
  string(2) "07"
  [7]=>
  string(2) "08"
  [9]=>
  string(2) "10"
  [10]=>
  string(2) "11"
  [11]=>
  string(2) "12"
}

You can then simply merge both, for example:

$exemplo = array( '01', '03', '06', '09');

$faltam = array_missing($exemplo);

$final = array_merge($exemplo, $faltam);

sort($final); //Ordena a array

var_dump($final);
  • 1

    @Leocaracciolo range from PHP itself makes it very simple.

1

One possibility to solve this is:

<?php 
$array_do_banco = array('01', '03', '04', '06', '11');
$array_saida = array();

for($i=1; $i<13; $i++){
    if(in_array($i, $array_do_banco)){
        $matches = array_keys($array_do_banco, $i);
        $array_saida[$i-1] = $array_do_banco[$matches[0]];
    }else{
        $array_saida[$i-1] = str_pad($i , 2 , '0' , STR_PAD_LEFT);
    }
}

var_dump($array_saida);
?>

The exit is:

array(12) { [0]=> string(2) "01" [1]=> string(2) "02" [2]=> string(2) "03" [3]=> string(2) "04" [4]=> string(2) "05" [5]=> string(2) "06" [6]=> string(2) "07" [7]=> string(2) "08" [8]=> string(2) "09" [9]=> string(2) "10" [10]=> string(2) "11" [11]=> string(2) "12" }

  • 2

    The output is https://ideone.com/sgGujr

  • 1

    @Leocaracciolo Interesting that ideone. Is that I do on localhost, so I had never used it hehe.

  • 1

    I like the one that can modify the code and click on Execute code without having to load the page each time. See http://sandbox.onlinephpfunctions.com/code/9ee559c13e7cd3410b085b0738513a97d14a34b6

  • 1

    To test small pieces of code is very interesting indeed. Thank you very much :)

Browser other questions tagged

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