我需要使用 php 将一个整数值分成 3 部分
例如:我有一个值 9 答案就像 3 + 3 + 3 如果我有值 10 像 3 + 3 + 4 或类似的答案
您可以使用模数函数。
<?php
$yourInt=10;
$remainder=$yourInt % 3;
$third=floor($yourInt/3);
$lastBit=$third+$remainder;
echo "The numbers are $third + $third + $lastBit.";
?>
输出:
The numbers are 3 + 3 + 4.
如果您的值 >= 3,则可以执行以下操作:
$number = 10;
$number1and2 = floor($number/3);
$number3 = $number - (2*$number1and2);
$num = 11;
$d = [$num/3,$num/3,$num/3];
$round = array_map('round', $d);
list($first, $second, $third) = $round;
$round = (is_float($num/3)) ? $num-(round($num/3)*3) : 0;
echo $first.' '.$second.' '.($third += (is_float($num/3)) ? $num-(round($num/3)*3) : 0);