【问题标题】:how to check how many times one number goes inside another number and create an array based on that in php?如何检查一个数字进入另一个数字的次数并在php中基于该数字创建一个数组?
【发布时间】:2015-01-28 21:24:12
【问题描述】:

假设我有:

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

有什么想法吗?

【问题讨论】:

  • 你得到什么输出?或者什么没有按预期工作?
  • segments 在第二个 for loop 中没有正确计算

标签: php arrays math foreach modulus


【解决方案1】:

使用取模运算符(确定除法的余数)然后计算 $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 借用的。

【讨论】:

  • 当然,但是你如何计算$totalLoop 给你正确的segment
  • 我用一个如何构建循环的示例扩展了我的初始回复。
  • 是的,我没有创建数组,我正在转储结果,如果结果正常,那么我会创建数组
【解决方案2】:

晚了,但我认为值得注意。我所做的只是在 foreach 循环中遍历数组并测试模块中的数字(在本例中为 100)。然后如果为真,增加一个 $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.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-16
    • 1970-01-01
    • 2014-05-17
    • 2011-12-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多