Home > Backend Development > PHP Tutorial > How to write recursive combination and arrangement function in PHP?

How to write recursive combination and arrangement function in PHP?

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-09-14 09:41:31
Original
1664 people have browsed it

php numbers are recursively combined and arranged:
$a="123,45,6789,...";//There is no limit to the number of digits in each segment. If it is a 4-segment number, the resulting combination will be 4 digits, and so on.
Need to get the combined array:
6,4,1
6,4,2
6,4,3
6,5,1
6,5,2
6,5,3
7,4,1
7 ,4,2
7,4,3
7,5,1
7,5,2
7,5,3
8,4,1
8,4,2
8,4,3
8,5 ,1
...
Looking for php recursive function

Reply content:

php numbers are recursively combined and arranged:
$a="123,45,6789,...";//There is no limit to the number of digits in each segment. If it is a 4-segment number, the resulting combination will be 4 digits, and so on.
Need to get the combined array:
6,4,1
6,4,2
6,4,3
6,5,1
6,5,2
6,5,3
7,4,1
7 ,4,2
7,4,3
7,5,1
7,5,2
7,5,3
8,4,1
8,4,2
8,4,3
8,5 ,1
...
Looking for php recursive function

<code>function recursion($groups, $echo = '')
{
    $current = array_pop($groups);
    $end = empty($groups);

    $echo .= $echo ? ',' : '';

    foreach (str_split($current) as $item) {
        $rEcho = $echo . $item;

        if ($end) {
            echo $rEcho . "\n";
        } else {
            recursion($groups, $rEcho);
        }
    }
}

recursion(explode(',', '123,45,6789'));</code>
Copy after login
Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template