DWQA QuestionsCategory: ProgramAn algorithm for splitting PHP arrays

Put the string \$str =’12,34,5′;
Split into array \$arr = [[1,3,5], [1,4,5], [2,3,5], [2,4,5]];
Find the PHP logic algorithm that converts \$str into \$arr;,To illustrate, using global may not seem so elegant, but here I am writing an example method, which is too tangled and can be optimized by myself

``````\$str = '12,34,5';
\$arr = explode(',', \$str);
\$step = \$book = \$result = [];
dfs(0);
print_r(\$result);

\$str = '12,34';
\$arr = explode(',', \$str);
\$step = \$book = \$result = [];
dfs(0);
print_r(\$result);

\$str = '12,34,5,67';
\$arr = explode(',', \$str);
\$step = \$book = \$result = [];
dfs(0);
print_r(\$result);
function dfs(\$s)
{
global \$arr, \$step, \$result, \$book;
if (!isset(\$arr[\$s])) {
\$result[] = array_values(\$step);
return;
}

for (\$i = 0; \$i < strlen(\$arr[\$s]); \$i++) {
if (!isset(\$book[\$s][\$i]) || \$book[\$s][\$i] == 0) {
\$book[\$s][\$i] = 1;
\$step[\$s] = \$arr[\$s][\$i];
dfs(\$s + 1);
\$book[\$s][\$i] = 0;
}
}
return;
}``````

,Two arrays can be well combined. Two nested for loops are sufficient. Most groups with uncertain numbers cannot be processed in this way. You can refer to the ideas of some sorting algorithms. Most groups are converted into two arrays by recursion, such as: [[1,2], [3,4], ], which are converted into [[13,14,23,24], ]. The last two nested for loops are solved. Reference code:

``````
\$str = '12,34,5';
\$arr = [];
foreach (explode(',', \$str) as \$v) {
\$arr[] = str_split(\$v);
}

print_r(fun(\$arr));

function fun(\$arr)
{
if (count(\$arr) >= 2) {
\$tmparr = [];
\$arr1 = array_shift(\$arr);
\$arr2 = array_shift(\$arr);
foreach (\$arr1 as \$v1) {
foreach (\$arr2 as \$v2) {
\$tmparr[] = \$v1 . \$v2;
}
}
array_unshift(\$arr, \$tmparr);
\$arr = fun(\$arr);
}
return \$arr;
}

``````

According to what law is this split?

To illustrate, using global may not seem so elegant, but here I am writing an example method, which is too tangled and can be optimized by myself

``````\$str = '12,34,5';
\$arr = explode(',', \$str);
\$step = \$book = \$result = [];
dfs(0);
print_r(\$result);

\$str = '12,34';
\$arr = explode(',', \$str);
\$step = \$book = \$result = [];
dfs(0);
print_r(\$result);

\$str = '12,34,5,67';
\$arr = explode(',', \$str);
\$step = \$book = \$result = [];
dfs(0);
print_r(\$result);
function dfs(\$s)
{
global \$arr, \$step, \$result, \$book;
if (!isset(\$arr[\$s])) {
\$result[] = array_values(\$step);
return;
}

for (\$i = 0; \$i < strlen(\$arr[\$s]); \$i++) {
if (!isset(\$book[\$s][\$i]) || \$book[\$s][\$i] == 0) {
\$book[\$s][\$i] = 1;
\$step[\$s] = \$arr[\$s][\$i];
dfs(\$s + 1);
\$book[\$s][\$i] = 0;
}
}
return;
}``````
Fire moon in the wind answered 2 months ago

Two arrays can be well combined. Two nested for loops are sufficient. Most groups with uncertain numbers cannot be processed in this way. You can refer to the ideas of some sorting algorithms. Most groups are converted into two arrays by recursion, such as: [[1,2], [3,4], ], which are converted into [[13,14,23,24], ]. The last two nested for loops are solved. Reference code:

``````
\$str = '12,34,5';
\$arr = [];
foreach (explode(',', \$str) as \$v) {
\$arr[] = str_split(\$v);
}

print_r(fun(\$arr));

function fun(\$arr)
{
if (count(\$arr) >= 2) {
\$tmparr = [];
\$arr1 = array_shift(\$arr);
\$arr2 = array_shift(\$arr);
foreach (\$arr1 as \$v1) {
foreach (\$arr2 as \$v2) {
\$tmparr[] = \$v1 . \$v2;
}
}
array_unshift(\$arr, \$tmparr);
\$arr = fun(\$arr);
}
return \$arr;
}

``````