The permutation and combination problem of PHP
Existing array $arr = Array (' area ', ' Income ', ' age ', ' Gender ');
To make a new array based on the elements in the above array, the following:
Areagenderincomeage
Areagenderageincome
Areaincomegenderage
Areaincomeagegender
Areaagegenderincome
Areaageincomegender
Genderincomeagearea
Genderincomeareaage
Genderageareaincome
Genderageincomearea
Genderareaageincome
Genderareaincomeage
Incomeagegenderarea
Incomeageareagender
Incomeareagenderage
Incomeareaagegender
Incomegenderareaage
Incomegenderagearea
Ageincomeareagender
Ageincomegenderarea
Ageareagenderincome
Ageareaincomegender
Agegenderareaincome
Agegenderincomearea
I hope you and the gods can provide good algorithms.
Share to:
------Solution--------------------
$arr = Array (' area ', ' Income ', ' age ', ' Gender ');
Print_r (Arrangement ($arr));
arranging arrangement
function arrangement ($arr = Array (), $res = ") {
if (! Is_array ($arr)) $arr = Str_split ($arr);
if (empty ($arr)) $array [] = $res;
else foreach ($arr as $k = = $v) {
Unset ($arr [$k]);
foreach (Arrangement ($arr, $res. "$v") as $t) $array [] = $t;
$arr [$k] = $v;
}
return $array;
}
Array
(
[0] = Area Income Age Gender
[1] = area Income Gender age
[2] = Area Age Gender Income
[3] = Area Age Income Gender
[4] = area Gender Income age
[5] = Area Gender Age Income
[6] = Income Age Gender Area
[7] = Income Age Area Gender
[8] = Income Gender area age