suppose i have:
$val = [1, 2, 3, 4, 5, 6, 7, 8];
$s = 3;
step 1: foreach $val
find how many times $s
is found in it. This is simple enough
foreach($val as $c){
if($c > $s) {
$total = floor($c / $s);
$remainder = $c % $s;
} else {
$total = floor($s / $c);
$remainder = $s % $c;
}
}
step 2: build an array that will show just that. For example:
// 3 doesn't go in 1
['segment' => 1]
// 3 doesn't go in 2
['segment' => 2]
// 3 goes once in 3
['segment' => 3]
// 3 goes once in 4 and reminder is 1
['segment' => 3]
['segment' => 1]
// 3 goes once in 5 and reminder is 2
['segment' => 3]
['segment' => 2]
// 3 goes twice in 6 and reminder is 0
['segment' => 3]
['segment' => 3]
// 3 goes twice in 7 and reminder is 1
['segment' => 3]
['segment' => 3]
['segment' => 1]
// 3 goes twice in 8 and reminder is 2
['segment' => 3]
['segment' => 3]
['segment' => 2]
and so on
i was trying something like the code below, but cant get it to work:
foreach($val as $c){
if($c > $s) {
$total = floor($c / $s);
$remainder = $c % $s;
$segment = $s;
} else {
$total = floor($s / $c);
$remainder = $s % $c;
$segment = $c;
}
$totalLoop = $c > $s ? $total + $remainder : 1;
var_dump('total: ' . $total . ', reminder: ' . $remainder . ', segment : ' . $segment);
for($i = 1; $i <= $totalLoop; $i++) {
$segment= $c > $s && $totalLoop == $i ? $remainder : $segment;
var_dump('segment: ' . $segment);
}
}
any ideas?