Note the problem of character string combination by experts (where the number of # and $ is not fixed, that is, the number of layers is not fixed, and it cannot be done with a fixed loop). The original string is as follows: a $ B $ c # A $ B $ C #1 $2 $3 need to combine N strings & nbsp; and use commas to connect aA1, aA2, aA3, aB1, aB2, aB3, aC1, aC2, and aC
Note (where the number of "#" and "$" is not fixed, that is, the number of layers is not fixed)
The original string is as follows:
A $ B $ c # A $ B $ C #1 $2 $3
Requires that N strings be combined and connected with commas
AA1, aA2, aA3, aB1, aB2, aB3, aC1, aC2, aC3, bA1, bA2, bA3, bB1, bB2, bB3, bC1, and so on.
Provide several strings:
A $ B $ c # A $ B $ C #1 $2 $3 # y $ u
A $ B $ c # A $ B $ C $ D $ E $ F #1 $2 $3 # y $ u #0 $9 $8 $7 $6 $5
The string is not fixed and there is no regularity.
------ Solution --------------------
$s = 'a$b$c#A$B$C#1$2$3';
foreach(explode('#', $s) as $v) {
$t[] = explode('$', $v);
}
echo join(',', foo($t));
function foo($ar) {
$t = array_shift($ar);
if(count($ar) > 1) {
$r = foo($ar);
}else $r = current($ar);
foreach($t as $t1) {
foreach($r as $r1) {
$res[] = $t1.$r1;
}
}
return $res;
}
AA1, aA2, aA3, aB1, aB2, aB3, aC1, aC2, aC3, bA1, bA2, bA3, bB1, bB2, bB3, bC1, bC2, bC3, A1, CA 2, a3, cB1, cB2, cB3, cC2, cC3