简体   繁体   English

数组中的多个组合

[英]multiple combinations in an Array

I currently have an array, the amount of items in the array can change. 我目前有一个数组,数组中的项目数量可以更改。 I am trying to find different combinations for the values inside the array, for example the array currently has these values; 我试图为数组中的值找到不同的组合,例如,数组当前具有这些值;

Array ( [0] => 60.0 [1] => 56.8 [2] => 42.08 [3] => 52.16 [4] => 52.8 )

Is it possible to count the number of values (in this case 5) then do something like this; 是否可以计算值的数量(在这种情况下为5),然后执行类似的操作;

60.0 + 56.8 60.0 + 56.8
60.0 + 56.8 + 42.08 60.0 + 56.8 + 42.08
60.0 + 56.8 + 42.08 + 52.16 60.0 + 56.8 + 42.08 + 52.16
60.0 + 56.8 + 42.08 + 52.16 + 52.8 60.0 + 56.8 + 42.08 + 52.16 + 52.8

But also to show all combinations such as; 而且还要显示所有组合,例如;
56.8 + 42.08 56.8 + 42.08
42.08 + 52.16 42.08 + 52.16
etc 等等

I have tried using multi dimensional arrays, array shifting and other array related code. 我尝试使用多维数组,数组移位和其他数组相关代码。

this example echos the sum and the equotation of the given array: 此示例回显给定数组的总和和引号:

$array = array(1,2,3,4);
$total = 0;
$equot = array();
foreach($array as $k=>$a) {     
   $total += $a;
   $equot[] = $a;
   if($k>0) { 
       echo implode("+",$equot)." = $total<br>"; 
   }
}

for the given array it echoes: 对于给定的数组,它回显:

  • 1+2 = 3 1 + 2 = 3
  • 1+2+3 = 6 1 + 2 + 3 = 6
  • 1+2+3+4 = 10 1 + 2 + 3 + 4 = 10

if you like to get all possible combinations you should read this existing thread: 如果您想获得所有可能的组合,则应阅读以下现有主题:

PHP: How to get all possible combinations of 1D array? PHP:如何获得一维数组的所有可能组合?

$sum = array_sum(array_slice($array, 0, rand(1, count($array))));

UPDATE: 更新:

The below code will accept an $array with the numbers, and then display the sum of random (starting from first, without duplicates) elements along with the equation. 下面的代码将接受带有数字的$array ,然后显示随机元素的总和(从第一个开始,没有重复项)以及等式。

$rand = rand(1, count($array));
$sum = array_sum(array_slice($array, 0, $rand));
$equation = implode(' + ', array_slice($array, 0, $rand));

echo $equation. ' = '. $sum;

In action: Codepad 实际操作: 键盘

This is just an example printing the equotation. 这只是打印报价单的示例。 If you want it to calculate, modify it. 如果要计算,请对其进行修改。

<?php

$array = Array(10, 20, 30, 40, 50);

for($i = 1; $i < count($array); $i++) {
    $sum = "";
    for($x = 0; $x <= $i; $x++) {
        $sum = $sum . $array[$x] . ($x != $i ? " + " : "");
    }
    echo $sum . "\r\n";
}

Working demo: http://codepad.org/ZgkdW2d5 工作演示: http : //codepad.org/ZgkdW2d5

Simple sum it up, then pop last element. 简单总结一下,然后弹出最后一个元素。

Will produce your expected output, just reversed. 将产生您的预期输出,只是反转。 (You can add the result to another array and resort, if required) (如果需要,您可以将结果添加到另一个数组并求助)

//untestet
while (count($myArray) > 1)){
  $current = 0;
  foreach ($myArray AS $e){
    $current += $e;
  }
  echo "A Result : " . $current;

  //remove last entry
  array_pop($myArray);
}

This however will modify your array. 但是,这将修改您的数组。 You could also do 你也可以

$myArray = Array(1,2,3);
for ($i=0; $i<count($myArray); $i++){
  $sums_until_index[$i] = 0;
  for ($k=0; $k<=$i; $k++){
    $sums_until_index[$i] += $myArray[$k];  
  }

}

print_r($sums_until_index); //Array ( [0] => 1 [1] => 3 [2] => 6 )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM