如何检查一个数字进入另一个数字的次数并根据php中的数据创建一个数组?

时间:2014-11-30 22:04:31

标签: php arrays math foreach modulus

假设我有:

$val = [1, 2, 3, 4, 5, 6, 7, 8];
$s = 3;

第1步:foreach $val查找在其中找到$s次的次数。这很简单

foreach($val as $c){
    if($c > $s) {
        $total       = floor($c / $s);
        $remainder   = $c % $s;
    } else {
        $total       = floor($s / $c);
        $remainder   = $s % $c;
    }
}

第2步:构建一个只显示该数组的数组。例如:

// 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]

等等

我正在尝试类似下面的代码,但无法让它工作:

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);
    }
}

任何想法?

2 个答案:

答案 0 :(得分:2)

使用模数运算符(确定除法的余数)然后计算$ s适合你的$ c的次数是我可以采用的最快方法。应该看起来像这样:

foreach ($val as $c) {
  $mod = $c % $s;
  $times = ($c-$mod)/$s;
}

$ times应该是你的结果。然后你应该能够使用$ times和$ mod:

构建你正在寻找的数组
$myArray = array();

while ($times > 0) {
    $myArray[] = array('segment' => $s);
    $times--;
}
$myArray[] = array('segment' => $mod);

看看你的第二个循环,似乎你现在总是只得到一个段,因为你似乎每次循环运行时都会覆盖变量。这是对的吗?

<强>更新

所以这些对我有用(鉴于我对你想要达到的目标的理解):

<?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);

?>

prettyPrint函数借用here

答案 1 :(得分:0)

但我认为这值得注意。我所做的只是在foreach循环中遍历数组,并测试模块的数字(在本例中为100)。然后,如果为true,则增加一个$ var,我们将看到数组中数字100出现了多少次:

$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.