I have a dynamically created array $array=[2,3,2]. I want to loop through it and get all the possible permutations.
$array = [2,3,2];
$c = count($array);
for($i=0; $i<$c; $i++) {
for($j=0; $j<$array[$i]; $j++) {
echo ($i+1).'-'.($j+1).'<br>';
}
}
The result should be something like:
1-1-1
1-1-2
1-2-1
1-2-2
1-3-1
1-3-2
2-1-1
2-1-2
2-2-1
2-2-2
2-3-1
2-3-2
but it returns that:
1-1
1-2
2-1
2-2
2-3
3-1
3-2
I hope this ll help you :
$array = [2, 3, 2];
for ($i = 1; $i <= $array[0]; $i++) {
for ($j = 1; $j <= $array[1]; $j++) {
for ($k = 1; $k <= $array[2]; $k++) {
echo $i,"-",$j,"-",$k,"<br>";
}
}
}
I am trying to create a 2D array where $multiples[$i] = array(multiples of $i), $i = 1,2,3...
function getMultiples($factor, $start = 0, 10)
{
$multiples = array();
for($i = $factor + $start; $i < 10; $i+=$factor)
$multiples[] = $i;
return $multiples;
}
for($i = 2; $i < 10; $i++)
{
$start = 0 ;
$multiples[$i] = getMultiples($i, $start, 10);
}
However, when I var_dump
$multiples[2] = array(0 => 2)
$multiples[3] = array(0 => 3)
$multiples[4] = array(0 => 4)
...
Each element of $values has been intialized with only the first multiple in each array.
I've tested this with non-numerical key values and it works fine. Static key values also work. The dynamic key value $i seems to the problem, what is going on here?
If you want to get the multiples of a number, you need to update the getMultiples as following:
function getMultiples($factor, $start = 0, $max)
{
$multiples = array();
for($i = 2; $i < $max; $i++)
$multiples[] = $i*$factor;
return $multiples;
}
What we change?
Loop 10 or $max time for($i = $factor + $start; $i < 10; $i+=$factor) to for($i = 2; $i < $max; $i++).
Find multiple $multiples[] = $i; to $multiples[] = $i*$factor;
I have code like this
$jumlahcolspan = array();//new array
$horizontaldeep = 5;
$level = array(5,4,3,8,7);//old array
for ($j = 0; $j < $horizontaldeep; $j++) {
$jml = 1;
for ($i = $j + 1; $i < $horizontaldeep; $i++) {
$jml = $level[$i] * $jml;
}
array_push($jumlahcolspan, $jml);
}
To put it simple, what I want to get is to multiply old array value which index start from $i+1 to the last and push it to another array.
So, its some thing like this
old array: [5, 4, 3, 8, 7]
new array: [4*3*8*7, 3*8*7, 7, 1]
I've tried this but it doesn't work also
for ($j = 0; $j < $horizontaldeep; $j++) {
$jml = 1;
for ($i = $j + 1; $i < $horizontaldeep; $i++) {
global $jml;
$jml = $level[$i] * $jml;
}
array_push($jumlahcolspan, $jml);
}
Tried this too but not work also.
for ($j = 0; $j < $horizontaldeep; $j++) {
array_push($jumlahcolspan, array_product(array_slice($level, $j+1)));
}
Note: now I'm reviewing my full code. May be something not right in my code.
I think the problem is related to $jml variable but I can't figure how to solve that. Can anyone help me?
One approach would be to use a Recursive Function to achieve that goal. The Recursive Function below demonstrates how. And, by the way, you may as well quick-test it here.
<?php
$oldArray = [5,4,3,8,7];
function arrayMatrixMultiply(array $old, array &$newArray=[]){
$result = 1;
foreach($old as $key=>$value){
if($key != 0){
$result*=$value;
}
}
$newArray[] = $result;
array_splice($old, 0, 1);
if(!empty($old)){
// JUST RECURSE TILL THE $oldArray BECOMES EMPTY
arrayMatrixMultiply($old, $newArray);
}
return $newArray;
}
$newArray = arrayMatrixMultiply($oldArray);
var_dump($newArray);
// PRODUCES::
array (size=5)
0 => int 672
1 => int 168
2 => int 56
3 => int 7
I have a question with grouping brackets together in a for loop. I use a for loop to store tournament results in array but would like to group the results by their rounds. I want to group each round. Take a look at the code below and you can compare how I would like to change the code to match the desired output:
$tournament_size = 16;
$upper_bracket_total_matches = $tournament_size - 1;
$lower_bracket_total_matches = $tournament_size - 2;
for($i = 1; $i <= $upper_bracket_total_matches; $i++)
{
$upper_brackets[]= "[0,0]";
}
$upper_bracket_results = implode(",", $upper_brackets)
/* Upper Bracket Output
[0,0],[0,0],[0,0],[0,0],[0,0],[0,0],[0,0],[0,0],
[0,0],[0,0],[0,0],[0,0],
[0,0],[0,0],
[0,0]
*/
/* Desired Upper Bracket Output
[[0,0],[0,0],[0,0],[0,0],[0,0],[0,0],[0,0],[0,0]],
[[0,0],[0,0],[0,0],[0,0]],
[[0,0],[0,0]],
[[0,0]]
*/
for($i = 1; $i <= $lower_bracket_total_matches; $i++)
{
$lower_brackets[]= "[0,0]";
}
$lower_bracket_results = implode(",", $lower_brackets);
/* Lower Bracket Output
[0,0],[0,0],[0,0],[0,0],
[0,0],[0,0],[0,0],[0,0],
[0,0],[0,0],
[0,0],[0,0],
[0,0],
[0,0]
*/
/* Desired Lower bracket Output
[[0,0],[0,0],[0,0],[0,0]],
[[0,0],[0,0],[0,0],[0,0]],
[[0,0],[0,0]],
[[0,0],[0,0]],
[[0,0]],
[[0,0]]
*/
Hope this is clear
thanks for any help
Karim.
Try this: it generates stuff based on the size:
<?php
$tournament_size = 8;
$rounds = log($tournament_size) / log(2);
$upper_bracket_results = array();
$lower_bracket_results = array();
$curr = $tournament_size;
for ($i = 0; $i <= $rounds; $i++) {
$inner = array();
for ($i2 = 0; $i2 < $curr; $i2++)
$inner[] = array(0, 0);
$curr /= 2;
$upper_bracket_results[] = $inner;
}
$curr = $tournament_size / 2;
for ($i = 0; $i < $rounds; $i++) {
$inner = array();
for ($i2 = 0; $i2 < $curr; $i2++)
$inner[] = array(0, 0);
$lower_bracket_results[] = $inner;
$lower_bracket_results[] = $inner;
$curr /= 2;
}
echo "Upper:\n\n";
echo json_encode($upper_bracket_results);
echo "\n\nLower:\n\n";
echo json_encode($lower_bracket_results);
Output:
Upper:
[[[0,0],[0,0],[0,0],[0,0],[0,0],[0,0],[0,0],[0,0]],[[0,0],[0,0],[0,0],[0,0]],[[0,0],[0,0]],[[0,0]]]
Lower:
[[[0,0],[0,0],[0,0],[0,0]],[[0,0],[0,0],[0,0],[0,0]],[[0,0],[0,0]],[[0,0],[0,0]],[[0,0]],[[0,0]]]
I am trying to create function that allows me to get all combinations of an array to later generate a list.
But my problem is that currently my function treat "ab" as different from "ba". I dont know how to explain it in words but I guess the picture below exemplify what I try to achieve.
function everyCombination($array) {
$arrayCount = count($array);
$maxCombinations = pow($arrayCount, $arrayCount);
$returnArray = array();
$conversionArray = array();
foreach ($array as $key => $value) {
$conversionArray[base_convert($key, 10, $arrayCount)] = $value;
}
for ($i = 0; $i < $maxCombinations; $i++) {
$combination = base_convert($i, 10, $arrayCount);
$combination = str_pad($combination, $arrayCount, "0", STR_PAD_LEFT);
$returnArray[] = strtr($combination, $conversionArray);
}
return $returnArray;
}
$a = everyCombination(array('a', 'b', 'c','d'));
print_r($a);
The desired ouput would be
a
ab
abc
abcd
b
bc
bcd
c
cd
d
What you need to do is iterate through the array recursively, for each recursion you should only iterate through all the greater elements. Like so:
function everyCombination($arr) {
$combos = array();
$len = count($arr);
for( $i=0; $i<$len; $i++) {
for( $j=$i+1; $j<=$len; $j++) {
$combos[] = implode("",array_slice($arr,$i,$j-$i));
}
}
return $combos;
}
Example call:
everyCombination(['a','b','c','d']);
Returns:
['a','ab','abc','abcd','b','bc','bcd','c','cd','d']
It seems that you're after consecutive results, so a double loop would be a better choice here; recursion requires more control.
function combos($array)
{
if (!$array) {
return [];
}
$n = count($array);
$r = [];
for ($i = 0; $i < $n; ++$i) {
$prefix = '';
for ($j = $i; $j < $n; ++$j) {
$r[] = $prefix . $array[$j];
$prefix .= $array[$j];
}
}
return $r;
}
print_r(combos([1, 2, 3, 4]));