I currently load numbers into an array, these are always different numbers and there can be any amount of them, here is an example;
Array ( [0] => 60.0 [1] => 56.8 [2] => 42.08 [3] => 52.16 [4] => 52.8 )
I am trying to calculate every possible out come of this array, I simply need to add all numbers together (apart from the key number and its self) and try to match a figure, for example;
60.0 + 56.8, 60.0 + 42.08, 60.0 + 52.16
etc etc
Then to 56.8 + 60.0, 56.8 + 42.08
etc etc
but not including; 60.0 + 60.0
or another calculations that include themselfs
try this one..
$str = Array ( "60.0","56.8","42.08","52.16","52.8" );
$result = array();
foreach ($str as $v){
foreach($str as $d){
if($d!=$v){
$result[]=$v."+".$d;
}
}
}
$strResult = implode(",",$result);
print_r($strResult);
I can offer the solution via grouping source values to pairs, like this:
function array_repeat_pair($rgData)
{
$rgRepeats = array();
for($i=0;$i<count($rgData);$i++)
{
for($j=0;$j<count($rgData);$j++)
{
if($i!=$j && !array_key_exists($i*$i+$j*$j, $rgRepeats))
{
$rgRepeats[$i*$i+$j*$j] = [$rgData[$i], $rgData[$j]];
}
}
}
return $rgRepeats;
}
$rgData = [60.0, 52.7, 54.2, 45.8];
$rgResult = array_map('array_sum', array_repeat_pair($rgData));
//var_dump($rgResult);
Not sure if it is exactly what you need (i.e. pair [a,b] is treated same as [b,a])
I'm not able to check it thoroughly at the moment, but I believe this will work:-
$array = array(60,56.8,42.08,52.16,52.8);
while(count($array) > 0){
$first = array_shift($array);
foreach($array as $value){
$result[] = $first + $value;
}
}
Output:-
array (size=10)
0 => float 116.8
1 => float 102.08
2 => float 112.16
3 => float 112.8
4 => float 98.88
5 => float 108.96
6 => float 109.6
7 => float 94.24
8 => float 94.88
9 => float 104.96