PHP permutations and combinations related issues

Source: Internet
Author: User
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
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.