## Your Location is: Home > Php

### how many new array to be born when i connect tow array in php

From: Sweden View: 4363 Minh Phong Đoàn

## Question

I have two arrays. The first array is

``````Array_A = [a, b, c, d, e, f]
``````

and the second array is

``````array_B = [0,1,2,3,4,5]
``````

Now I want to count how many new arrays consisting of 6 words are created from combining two arrays array_A, array_b, provided that 6 quarters in each of these new arrays do not overlap. for example the newly created array

``````array1 = [a0, b1, c2, d3, e4, f5]
array2 = [a0, b2, c1, d4, e5, f2]
array3 = [a0, b3, c2, d5, e4, f1]
.
.
etc.
``````

I want to count how many arrays like this are generated

By considering that both the arrays have same count like 6 in above exmaple :

case 1: If we want the Array_A (or any one array) elements to be first always The total number combinations would be (count)! As the count is 6 in our scenario, the total combinations are 6! = 720 combinations

case 2: If the first element of combination can be from any array like a0 or 0a The total number of combinations would be (count)! * 2 As the count is 6 in our scenario, the total combinations are 6! * 2 = 720 * 2 = 1440 combinations example arrays : [a0,b1,c2,d3,e4,f5] or [0a,1b,2c,3d,4e,5f].

Note : ! - is a factorial symbol, Where `n! = n*(n-1)*(n-2)......3*2*1`

PHP code to print all the arrays that can be formed

``````<?

\$arrayB = array(0, 1, 2, 3, 4, 5);
\$arrayBPermutations = permutationsOfAnArray(\$arrayB);
\$combinedArray = array();
\$arrayA = array('a', 'b', 'c', 'd', 'e', 'f');
for (\$i = 0; \$i < count(\$arrayBPermutations); \$i++) {
for (\$j = 0; \$j < count(\$arrayA); \$j++) {
\${'combinedArray' . \$i}[\$j] = \$arrayA[\$j] . \$arrayBPermutations[\$i][\$j];
}
}
for (\$i = 0; \$i < count(\$arrayBPermutations); \$i++) {
if (\$i > 0 && \${'combinedArray' . \$i} == \${'combinedArray' . (\$i - 1)}) {
error_log("found duplicate", 3, "/var/log/app/duplicates.log");
}
print_r(\${'combinedArray' . \$i});
}

//Swap Values
function swap(&\$arr, \$a, \$b)
{
\$temp = \$arr[\$a];
\$arr[\$a] = \$arr[\$b];
\$arr[\$b] = \$temp;
}

//Find Factorial of \$n
function factorial(\$n)
{
\$val = 1;
for (\$i = 1; \$i < \$n; \$i++) {
\$val *= \$i;
}
return \$val;
}

/**
* All permutations of \$arrayToPermutate
* @param \$arrayToPermutate
* @return array
*/
function permutationsOfAnArray(\$arrayToPermutate)
{
\$total = factorial(count(\$arrayToPermutate));
\$count = 0;
\$arrayWithAllPerm = array();
\$arrPermIndex = 0;
for (\$j = 0, \$i = 0, \$inc = 1; \$j < \$total; \$j++, \$inc *= -1, \$i += \$inc) {

for (; \$i < count(\$arrayToPermutate) - 1 && \$i >= 0; \$i += \$inc) {
//store permutation in an array
\$arrayWithAllPerm[\$arrPermIndex] = \$arrayToPermutate;
\$arrPermIndex++;
swap(\$arrayToPermutate, \$i, \$i + 1);
\$count++;
}
//store permutation in an array
\$arrayWithAllPerm[\$arrPermIndex] = \$arrayToPermutate;
\$arrPermIndex++;
\$count++;

if (\$inc === 1) {
swap(\$arrayToPermutate, 0, 1);
} else {
swap(\$arrayToPermutate, count(\$arrayToPermutate) - 1, count(\$arrayToPermutate) - 2);
}
}
//print total permutations
echo "Total count of permutaions is " . \$count;
return \$arrayWithAllPerm;
}

?>
``````

We can verify "/var/log/app/duplicates.log" file to check if the script is creating an duplicate arrays.

"Total count of permutaions is" printed to check the total count.