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

Get the solution ↓↓↓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

### Answer

#### Solution:

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.

If we want to store all permutations of one array like arrayB = array('a','b','c','d','e'), use below code.

```
<?
$arrayB = array('a', 'b', 'c', 'd', 'e', 'f');
$arrayBPermutations = permutationsOfAnArray($arrayB);
for ($i = 0; $i < count($arrayBPermutations); $i++) {
${'a' . $i} = $arrayBPermutations[$i];
}
for ($i = 0; $i < count($arrayBPermutations); $i++) {
if ($i > 0 && ${'a' . $i} == ${'a' . ($i - 1)}) {
error_log("found duplicate", 3, "/var/log/app/duplicates.log");
}
print_r(${'a' . $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;
}
?>
```

All the permutations are stored in $a0, $a1, $a2,...... variables.

**Share solution ↓**

### Additional Information:

**Date the issue was resolved:**

Link To Answer People are also looking for solutions of the problem: 403 this action is unauthorized.

### Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

### Similar questions

Find the answer in similar questions on our website.

### Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.