How to separate array in odd/even and calculate multiple number?

Asked

Viewed 1,736 times

3

I’m making a script to separate odd number and even number of an array inserted via form option, and make specific calculation on them (to separate odd number from pair array I’m using explode and implode to put them all on the same line, but my code is incomplete).

My question is: How to calculate so that in the final result the value is rounded to a multiple number of 10?

EXAMPLE: final result: 135

to be multiple of 10, missing 5 , totals 140.

Code:

<?php
//separa os numeros da entrada da array retirando espaços vazios
$numUPC = explode(',', trim(trim($final_array), ',')); 
//final_array gerando erro undefined, motivo variavel sem entrada (Vazia)
 
$UPCdigito = array();
$numUPCpar = array();
$numUPCimpar = array();
$numArredondado = array();
 
//filtor de numero par e impar
foreach ($numUPC as $key => $value) {
    if ($key % 2 == 0) {
        $numUPCpar[] = $value;
    } else {
        $numUPCimpar[] = $value;
    }
}
 
$numUPCpar  = implode(', ', $numUPCpar);
$numUPCimpar = implode(', ', $numUPCimpar);
 
//calculo
$numUPCimpar = array_sum($numUPCimpar);
$numUPCpar = array_sum($numUPCpar);
$UPCdigito = $numUPCimpar * 3;
$UPCdigito = $UPCdigito + $numUPCpar;
$numArredondado = (round($UPCdigito / 10, 0) )* 0.5
 
?>

Is the odd and even number filter logic correct? And how do I identify in my final result how much longer for the number to be multiple of 10?

I tried to use round, but it is vacant.

  • 1

    You can give an example of the input array?

2 answers

2

Even or odd check is correct. To round to multiples of 10 you can do so:

<?php
function round_multiple10_up($num) {
    $mod = $num % 10;

    if ($mod !== 0)
        return (10 - $mod) + $num;

    return $num;
}

function round_multiple10_down($num) {
    $mod = $num % 10;

    if ($mod !== 0)
        return $num - $mod;

    return $num;
}


$test = 144;

echo round_multiple10_up($test); //150
echo PHP_EOL;
echo round_multiple10_down($test); //140

You can test this code at this link: http://ideone.com/L74t8w

0

/**
O array original com os números
*/
$arr = [11,22,31,41,53,67,71,89,90,106];

/**
Organiza os números ímpares e pares num array.
O termo "even" significa "par", o termo "odd" significa "ímpar".
*/
foreach ($arr as $v)
    $rs[(($v % 2 == 0)? 'even' : 'odd')][] = $v;

/**
Verifica se existe o índice de números pares e aplica a soma total e o arredondamento para um múltiplo de 10. O arredondamento é sempre para cima.
*/
if (isset($rs['even']))
    $rs['sum']['even'] = ceil(array_sum($rs['even']) / 10) * 10;

/**
Verifica se existe o índice de números ímpares e aplica a soma total e o arredondamento para um múltiplo de 10. O arredondamento é sempre para cima.
*/
if (isset($rs['odd']))
    $rs['sum']['odd'] = ceil(array_sum($rs['odd']) / 10) * 10;

/**
Imprime o resultado, para depuração.
*/
print_r($rs);

Browser other questions tagged

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