示例#1
1
function combinations($arrays, $i = 0)
{
    if (!isset($arrays[$i])) {
        return [];
    }
    if ($i == count($arrays) - 1) {
        return $arrays[$i];
    }
    $c = combinations($arrays, $i + 1);
    $result = [];
    foreach ($arrays[$i] as $v) {
        foreach ($c as $t) {
            $result[] = is_array($t) ? array_merge([$v], $t) : [$v, $t];
        }
    }
    return $result;
}
function combinations($arrays, $i = 0)
{
    if (!isset($arrays[$i])) {
        return array();
    }
    if ($i == count($arrays) - 1) {
        return $arrays[$i];
    }
    // get combinations from subsequent arrays
    $tmp = combinations($arrays, $i + 1);
    $result = array();
    // concat each array from tmp with each element from $arrays[$i]
    foreach ($arrays[$i] as $v) {
        foreach ($tmp as $t) {
            $result[] = is_array($t) ? array_merge(array($v), $t) : array($v, $t);
        }
    }
    return $result;
}
示例#3
1
                     }
                 }
             }
         }
     }
     return $main_array;
 }
 /*
 	Make sure at some point to separate one robot's json from another
 	And sell coins to buy robots that can listen to sentence that are 20 to 30 words long
 	Robots can't interact with each other in the same room. Have user pick which robot to talk to on login.
 	Robots must be named. The name in combination with the Facebook user id key will identify the robot.
 	No two robots belonging to anyonne can have the same name. If a name is taken, popup an alert.
 	Bot name should not exceed 20 characters.
 */
 $the_array = combinations($comb1, $row);
 foreach ($the_array as $combinations_for_table) {
     $encode = json_encode($combinations_for_table);
     print_r($the_array);
     if (count($icons_written) > 0 && count($the_array) > 0) {
         $encode2 = json_encode($icons_written);
         $sql_select = "SELECT * FROM `json_additions` WHERE `addition`='" . $encode . "' AND `icons`='" . $encode2 . "'";
         $result_select = mysqli_query($conn, $sql_select);
         $item_with_less = json_decode($row2["addition"]);
         for ($g = 0; $g < count($item_with_less); $g++) {
             $added_blank = array_splice($item_with_less, $g, 0, "____");
             $encode3 = json_encode($added_blank);
             $sql_select2 = "SELECT * FROM `json_additions` WHERE addition='" . $encode3 . "' AND icons='" . $encode2 . "'";
             $result_select2 = mysqli_query($conn, $sql_select2);
             while ($row2 = mysqli_fetch_assoc($result_select2)) {
                 if ($added_blank == $combinations_for_table && mysqli_num_rows($result_select) == 0) {
function combinations($items, $num)
{
    $returnArray = array();
    if (sizeof($items) == $num) {
        // required = remaining --> return that array
        return array($items);
        // make sure this is a copy
    }
    if ($num == 0) {
        // number of items required has been reached -> return empty array
        return array();
    }
    // otherwise
    $item = array(array_shift($items));
    $temp = combinations($items, $num - 1);
    $partA = array();
    if (sizeof($temp) == 0) {
        $partA = array($item);
    }
    foreach ($temp as $t) {
        if (is_array($t)) {
            $partA[] = array_merge($item, $t);
        } else {
            $partA[] = array_merge($item, array($t));
        }
    }
    $partB = combinations($items, $num);
    echo "Boop" . rand(1, 100) . "\n";
    return array_merge($partA, $partB);
}
示例#5
0
function combinations(&$options, $options_seperator = '|', $pos = 0, $combinations = false)
{
    $new_combinations = array();
    if ($combinations) {
        foreach ($options[$pos]['values'] as $key1 => $value1) {
            foreach ($combinations as $key2 => $value2) {
                $new_combinations[$key2 . $options_seperator . $key1]['comb_name'] = $value2['comb_name'] . '; ' . $options[$pos]['comb_name'] . ': ' . $value1;
                $new_combinations[$key2 . $options_seperator . $key1]['comb_price'] = $value2['comb_price'] + ($options[$pos]['price_prefix'][$key1] == '+' ? 1 : -1) * $options[$pos]['options_values_price'][$key1];
                $new_combinations[$key2 . $options_seperator . $key1]['option_value'] = $value2['option_value'];
                $new_combinations[$key2 . $options_seperator . $key1]['option_value'][$options[$pos]['comb_name']] = $value1;
            }
        }
    } else {
        //This is a modification of the above for the first pass
        foreach ($options[$pos]['values'] as $key => $value) {
            $new_combinations[$key]['comb_name'] = $options[$pos]['comb_name'] . ': ' . $value;
            $new_combinations[$key]['comb_price'] = ($options[$pos]['price_prefix'][$key] == '+' ? 1 : -1) * $options[$pos]['options_values_price'][$key];
            $new_combinations[$key]['option_value'][$options[$pos]['comb_name']] = $value;
        }
    }
    if (++$pos < sizeof($options)) {
        return combinations($options, $options_seperator, $pos, $new_combinations);
    } else {
        return $new_combinations;
    }
}