PHP从数据中提取所有可能的组合

时间:2019-03-14 14:15:26

标签: php loops recursion iterator

我想提取以下数组($ OriginalData)中数据的所有可能变化,以了解如何在第二数组($ Variations)中进行布局。

在这种情况下,有3个问题,但是这个数字是可变的。问题1有3个选项,问题2有5个选项,问题3有2个选项。

我需要获取一个对象数组,其中包含3个问题的所有可能答案组合。在这种情况下,我认为应该有30种可能的组合。我已经在$ Variations数组中提供了预期的输出(对于前3个组合)。

任何帮助都将不胜感激!

$OriginalData = array();
$OriginalData[] = (object) array(
    'ID'=>51,
    'Options'=>array(
        array('OptionID'=>27396,'Surcharge'=>50),
        array('OptionID'=>27397,'Surcharge'=>40),
        array('OptionID'=>27398,'Surcharge'=>45),
    )
);

$OriginalData[] = (object) array(
    'ID'=>52,
    'Options'=>array(
        array('OptionID'=>27383,'Surcharge'=>5),
        array('OptionID'=>27384,'Surcharge'=>5),
        array('OptionID'=>27385,'Surcharge'=>5),
        array('OptionID'=>27386,'Surcharge'=>5),
        array('OptionID'=>27387,'Surcharge'=>5),
    )
);

$OriginalData[] = (object) array(
    'ID'=>53,
    'Options'=>array(
        array('OptionID'=>27343,'Surcharge'=>12),
        array('OptionID'=>27344,'Surcharge'=>15),
    )
);

print_r($OriginalData);


$Variations[0]['Options'][51] = (object) array('OptionID'=>27396,'Surcharge'=>50);
$Variations[0]['Options'][52] = (object) array('OptionID'=>27383,'Surcharge'=>5);
$Variations[0]['Options'][53] = (object) array('OptionID'=>27343,'Surcharge'=>12);  
$Variations[0]['Summary'] = (object) array('TotalSurcharge'=>67);   

$Variations[1]['Options'][51] = (object) array('OptionID'=>27397,'Surcharge'=>40);
$Variations[1]['Options'][52] = (object) array('OptionID'=>27383,'Surcharge'=>5);
$Variations[1]['Options'][53] = (object) array('OptionID'=>27343,'Surcharge'=>12);  
$Variations[1]['Summary'] = (object) array('TotalSurcharge'=>57);   

$Variations[2]['Options'][51] = (object) array('OptionID'=>27398,'Surcharge'=>45);
$Variations[2]['Options'][52] = (object) array('OptionID'=>27383,'Surcharge'=>5);
$Variations[2]['Options'][53] = (object) array('OptionID'=>27343,'Surcharge'=>12);  
$Variations[2]['Summary'] = (object) array('TotalSurcharge'=>62);   

print_r($Variations);

1 个答案:

答案 0 :(得分:0)

这是一个解决方案:

$OriginalData = array();
$OriginalData[] = (object) array(
    'ID' => 51,
    'Options' => array(
        array('OptionID' => 27396, 'Surcharge' => 50),
        array('OptionID' => 27397, 'Surcharge' => 40),
        array('OptionID' => 27398, 'Surcharge' => 45),
    )
);

$OriginalData[] = (object) array(
    'ID' => 52,
    'Options' => array(
        array('OptionID' => 27383, 'Surcharge' => 5),
        array('OptionID' => 27384, 'Surcharge' => 5),
        array('OptionID' => 27385, 'Surcharge' => 5),
        array('OptionID' => 27386, 'Surcharge' => 5),
        array('OptionID' => 27387, 'Surcharge' => 5),
    )
);

$OriginalData[] = (object) array(
    'ID' => 53,
    'Options' => array(
        array('OptionID' => 27343, 'Surcharge' => 12),
        array('OptionID' => 27344, 'Surcharge' => 15),
    )
);

$variations = array();
$counts = array();
$combinations = [[]];
$length = count($OriginalData);
for ($i = 0; $i < $length; $i++) {
    $counts[] = range(0, count($OriginalData[$i]->Options) - 1);
}
for ($count = 0; $count < $length; $count++) {
    $tmp = [];
    foreach ($combinations as $v1) {
        foreach ($counts[$count] as $v2)
            $tmp[] = array_merge($v1, [$v2]);
    }
    $combinations = $tmp;
}
$x = 0;
foreach ($combinations as $c) {
    $totalsurcharge = 0;
    for ($i = 0; $i < $length; $i++) {
        $Variations[$x]['Options'][$OriginalData[$i]->ID] = (object) $OriginalData[$i]->Options[$c[$i]];
        $totalsurcharge += $OriginalData[$i]->Options[$c[$i]]['Surcharge'];
    }
    $Variations[$x]['Summary'] = (object) array('TotalSurcharge' => $totalsurcharge);
    $x++;
}

var_dump($Variations);