Reputation: 27048
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?
Upvotes: 0
Views: 6834
Reputation: 43
Late but I thought it noteworthy. All I did was iterate through the array in a foreach loop and test the modular for a number (in this case 100). Then if true, increment a $var and we'll have how many times the number 100 shows up in the array:
$o=0;
$var=0;
foreach($video_tags as $video_tagss)
{
if($o === 1) //skip this number as it always comes up with a hit.
{
}
else
{
if($o % 100 == 1) //is the number a multiple of 100?
{
$var = $var + 1; //if yes, increment $var.
}
}
$o++;
}
// $video_tags had a 390 item count, and $var returned number 3.
Upvotes: 0
Reputation: 1271
Using the modulus operator (which determines the remainder of the division) and then calculating how many times $s fits in your $c is the quickest approach I could muster. Should look something like this:
foreach ($val as $c) {
$mod = $c % $s;
$times = ($c-$mod)/$s;
}
$times should be your result. You should then be able to build the array you're looking for with $times and $mod:
$myArray = array();
while ($times > 0) {
$myArray[] = array('segment' => $s);
$times--;
}
$myArray[] = array('segment' => $mod);
Looking at your second loop, it seems to that you are currently always only getting exactly one segment, as you seem to be overwritin the variable every time the loop runs. Is that correct?
UPDATE:
So here's what works for me (given my understanding of what you want to achieve):
<?php
$val = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16];
$s = 3;
// Initialize array
$myArray = array();
// loop over each element in $var
foreach ($val as $c) {
// get remainder of divisoin by $s
$mod = $c % $s;
// calculate number of times $s fits into $c
$times = ($c-$mod)/$s;
// add a segment with value of $s to $myArray for each number of times it fits into $c
while ($times > 0) {
// adding an index with value of $c when adding to $myArray
$myArray[$c][] = array('segment' => $s);
// reducing the counter $times in loop
$times--;
}
// output a last segment with value of remainder of division, unless the remainder is 0
if ($mod != 0) {
$myArray[$c][] = array('segment' => $mod);
}
}
// function for pre-formatted output
function prettyPrint($a) {
echo '<pre>'.print_r($a,1).'</pre>';
}
// show results
prettyPrint($myArray);
?>
The prettyPrint
function is borrowed from here.
Upvotes: 5