给定一个整数为310,我希望这个整数像这样分割
在数组中存储时应该是这样的
arr[0] = 100;
arr[1] = 100;
arr[2] = 100;
arr[3] = 10;
编辑:我知道如何使用array_fill,但我需要一个不使用内置功能的解决方案,如array_fill
这个简单的函数可以帮助您将任何值分解为所需值的片段数。
<?php
$input = 310; // 100, 100, 100, 10
/**
* Breaks the number into summable pieces of array
* @param $input int
* @param $pcsvalue int
* @return $pieces array
* Eg.
* $input = 310
* $pcsvalue = 100
* will be broken into 100, 100, 100, 10
* or some other given piece value.
* final output = array(100, 100, 100, 10)
*/
function breakNumberIntoSmallPieces($input = 0, $pcsvalue) {
$pieces = array();
if($input > 0) {
$breakintovalueof = $pcsvalue;
while(($op = $input - $breakintovalueof) > 0) {
$pieces[] = $breakintovalueof;
$input = $op;
}
$pieces[] = 100 + $op; // added into final op value because final op might be in negative value
} else {
$pieces[] = $input;
}
return $pieces;
}
$result = breakNumberIntoSmallPieces($input, 100);
echo '<pre>';
print_r($result);
?>
输出:Array
(
[0] => 100
[1] => 100
[2] => 100
[3] => 10
)
另一个简洁明了的解决方案是:
<?php
$input = 310;
$pcsvalue = 100;
$pieces = [];
$remainder = $input % $pcsvalue; // returns 10
$times = (int) ($input / $pcsvalue); // returns 3 by converting 3.1 to 3
$i = 1;
while($i <= $times){ // add 100 3 times into array
$pieces[] = $pcsvalue;
$i++;
}
if($remainder) {
$pieces[] = $remainder;
}
echo '<pre>';
print_r($pieces);
?>
Output:
Array
(
[0] => 100
[1] => 100
[2] => 100
[3] => 10
)
不使用array_fill:
$num = 310;
$slash = 100;
$array = [];
$mod = $num%$slash;
for($i=0;$i<(int)($num/$slash);$i++){
$array[] = $slash;
}
if($mod) {
$array[] = $mod;
}
我不明白你真正的问题,在一些假设我提供一些代码,你可能会使用
<?php
$arr=array();
$num=310;
while($num>=100)
{
array_push($arr,100);
$num=$num-100;
}
array_push($arr,$num);
print_r($arr);
?>
用下面的函数包装
<?php
$number = 310;
$splitAmount = 100;
$splittedArray = splitNumberInArray($number, $splitAmount);
var_dump($splittedArray);
function splitNumberInArray($number, $splitAmount) {
$returnArray = array();
while($number > 0) {
if($number < $splitAmount)
$splitAmount = $number;
$number -= $splitAmount;
array_push($returnArray, $splitAmount);
}
return $returnArray;
}
/*
result:
array (size=4)
0 => int 100
1 => int 100
2 => int 100
3 => int 10
*/
?>
试试这个:(现在甚至负数都是受欢迎的!)
<?php
function breakTheNum($number, $breakWith = 100) {
$float = $number / $breakWith;
$count = floor($float);
$outarr = array();
if($count > 0) {
for($i=0; $i<$count; $i++) {
$outarr[] = 100;
}
$outarr[] = ($float - $count) * $breakWith;
}
elseif($count < 0) {
for($i=0; $i>$count+1; $i--) {
$outarr[] = -100;
}
$outarr[] = ($float - ($count+1)) * $breakWith;
}
else {
$outarr[0] = $float * $breakWith;
}
return $outarr;
}
$array = breakTheNum(334);
print_r($array);
?>
结果:
Array ( [0] => 100 [1] => 100 [2] => 100 [3] => 34 )
例如breakTheNum(-534)
:
Array ( [0] => -100 [1] => -100 [2] => -100 [3] => -100 [4] => -100 [5] => -34 )
/**
* @param $number
* @param $step
* @return array
*/
function break_into_peaces($number, $step) {
$array = array();
while($number >= $step) {
array_push($array, $step);
$number = $number-$step;
}
array_push($array, $number);
return $array;
}
var_dump(
break_into_peaces(334, 100) // 100, 100, 100, 34
);
var_dump(
break_into_peaces(334, 50) // 50, 50, 50, 50, 50 , 50, 34
);