如何检索数据库结果数组并进行所有可能的结对组合?

时间:2016-01-16 17:26:44

标签: php arrays mysqli combinations

请有人帮助我:

这是代码:

 <?php
 //...db connection code ...

 function icecream($flavs, $size, $combinations = array()) {
    if (empty($combinations))
    {
       $combinations = $flavs;
    }

    if ($size == 1)
    {
       return $combinations;
    }

    $new_combinations = array();

    foreach ($combinations as $combination)
    {
       foreach ($flavs as $flav)
       {
          $new_combinations[] = $combination .' - '. $flav;
       }
    }

    return icecream($flavs, $size - 1, $new_combinations);
 }

 $sql = ("SELECT name FROM eissorten");
 $result = mysqli_query($conn, $sql);

 while ($row = mysqli_fetch_array($result))
 {
    $ice_all[] = $row['name'];
 }
 $ice_all = implode(', ',$ice_all);

 $flavs = array('Vanilla', 'Strawberry', 'Chocolate', 'Stracciatella');
 $output = icecream($flavs, 2);

 foreach($output as $outputs)
 {
    echo $outputs.'<br>';
 }

 ?>

它给了我所有可能的风味组合:

 $flavs = array('Vanilla', 'Strawberry', 'Chocolate', 'Stracciatella');

但我需要在数据库中使用$ row [&#39; name&#39;]填充数组,所以当我这样做时:

 $flavs = array($ice_all);

然后它给了我完整的口味。 我做错了什么?

0 个答案:

没有答案