Good morning
I have the result in MYSQL
01
03
04
06
11
I need a solution that fills in 2 zeros where it loses the numerical sequence up to number 12.
Example:
01
00
03
04
00
06
00
00
00
00
11
00
Good morning
I have the result in MYSQL
01
03
04
06
11
I need a solution that fills in 2 zeros where it loses the numerical sequence up to number 12.
Example:
01
00
03
04
00
06
00
00
00
00
11
00
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] = '00';
}
}
var_dump($array_saida);
?>
The output is:
array (12) {[0] = > string (2) "01" [1] = > string (2) "00" [2] = > string (2) "03" [3] = > string (2) "04" [4] = > string (2) "00" [5] = > string (2) "06" [6] = > string (2) "00" [7] = > string (2) "00" [8] = > string (2) "00" [9] = > string (2) "00" [10] = > string (2) "11" [11] = > string (2) "00"}
I think a solution would be:
$mysql = ['01', '03', '04', '06', '11'];
$final = array_fill(0, max(12, max($mysql)), '00');
foreach($mysql as $n){
$final[(int)$n-1] = $n;
}
var_dump($final);
Result:
array(12) {
[0]=>
string(2) "01"
[1]=>
string(2) "00"
[2]=>
string(2) "03"
[3]=>
string(2) "04"
[4]=>
string(2) "00"
[5]=>
string(2) "06"
[6]=>
string(2) "00"
[7]=>
string(2) "00"
[8]=>
string(2) "00"
[9]=>
string(2) "00"
[10]=>
string(2) "11"
[11]=>
string(2) "00"
}
The idea is very simple, we create with 12 values, with all 00
, using array_fill
. Then we just replace the values based on $mysql
.
Since $mysql
has values in string
we can not use it directly in the array, so we pass to int using (int)$n
. In this way the value 03
would be as final $ [3] = '03'.
max(12, max($mysql))
was added to generate 12 values, as requested in your question. However, some value of $mysql
extrapolate to 12 will be used as such. In this situation, if it is $mysql = ['01', '111'];
it will still work.
Although I personally would not use it, there is another way, even without using if
, foreach
or while
explicitly:
$resultado_do_mysql = ['01', '03', '04', '06', '11'];
$final = array_replace(array_fill(1, 12, '00'), array_combine(array_map('intval', $mysql), $mysql));
var_dump($final);
What we do in this case is to create another array, so we make the value go from string to int (using array_map('intval', $mysql)
). So we use this value int as an index of the array, still keeping the original value using array_combine(array_map('intval', $mysql), $mysql)
. Finally, we replace the value of $final
with the value of that array, using array_replace
. I believe this is more confusing than the first method.
Friend,
You can do something in this line of the code below, which commented:
<?php
$numeros = array("01", "03", "04", "06", "11"); // que vieram do banco
// criamos uma função para adicionar o zero na frente do número
function zeroes($n) {
return ((int) $n) < 10 ? "0{$n}" : "{$n}";
}
// aqui fazemos o for/loop para agregar as 12 entradas no array $resultado
$resultado = array();
for($i=1; $i<=12; $i++) {
// usamos o in_array para saber se o teu numero existe no array original
if(in_array($numeros, zeroes($i))) {
// se tiver, adicionamos ele na sequencia
$resultado[] = zeroes($i);
} else {
// caso não adicionamos o valor "00"
$resultado[] = "00";
}
}
print_r($resultado);
The basic concept here is in a loop from 1 to 12, check if the current loop number is between the numbers that came from your bank, if not, add 00 in the sequence, if the original number remains ... more or less there.
A more simplified example would be to get only the highest value and with a loop checking what exists, the logic would be to use the array from the bank only to know how many loops are needed by taking the maximum value in the array with max()
, then if it is greater than 12, it will have more than 12 cycles, if it is smaller it will have exactly 12 cycles, for example:
<?php
$mysql = array( '01', '03', '04', '06', '11' );
//O max dentro verifica qual o maior numero, o outro verifica se é maior ou não que 12
$max = max(12, max($mysql));
//Será o resultado final
$final = array();
//Faz um loop baseado no valor máximo
for ($i = 1; $i <= $max; $i++) {
//Gera o valor atual
$current = str_pad($i, 2, '0', STR_PAD_LEFT);
//Checa se o valor atual existe no array que veio do banco
if (in_array($current, $mysql)) {
$final[] = $current;
} else { //Se não existir preenche com 2 ZEROs
$final[] = '00';
}
}
print_r($final);
The loop starts at 1
because it prevents us from having to do something like $i-1
within for
, example at ideone