如何检查一个数字进入另一个数字的次数并在php中基于该数字创建一个数组?

Pat*_*cow 1 php arrays math foreach modulus

假设我有:

$val = [1, 2, 3, 4, 5, 6, 7, 8];
$s = 3;
Run Code Online (Sandbox Code Playgroud)

step 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;
    }
}
Run Code Online (Sandbox Code Playgroud)

第 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]
Run Code Online (Sandbox Code Playgroud)

等等

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

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

mat*_*jay 5

使用模数运算符(确定除法的余数)然后计算 $s 适合 $c 的次数是我能想到的最快方法。应该是这样的:

foreach ($val as $c) {
  $mod = $c % $s;
  $times = ($c-$mod)/$s;
}
Run Code Online (Sandbox Code Playgroud)

$times 应该是你的结果。然后,您应该能够使用 $times 和 $mod 构建您要查找的数组:

$myArray = array();

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

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

更新:

所以这对我有用(考虑到我对你想要实现的目标的理解):

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

?>
Run Code Online (Sandbox Code Playgroud)

prettyPrint函数是从这里借用的。