<?PHP$source=Array(' PLL ', ' I ', ' love ', ' You ', ' Hey ');Sort($source);//ensure that the initial array is ordered$last=Count($source)-1;//$source trailing element subscript$x=$last;$count= 1;//number of combinations statisticsEcho implode(‘,‘,$source), "<br>";//output of the first combination while(true) { $y=$x--;//two elements adjacent to each other if($source[$x] <$source[$y]) {//if the value of the previous element is less than the value of the next element $z=$last; while($source[$x] >$source[$z]) {//starting at the tail, find the first value greater than $x element $z--; } /*exchanging values for $x and $z elements*/ List($source[$x],$source[$z]) =Array($source[$z],$source[$x]); /*reverse all elements after $y*/ for($i=$last;$i>$y;$i--,$y++) { List($source[$i],$source[$y]) =Array($source[$y],$source[$i]); } Echo implode(‘,‘,$source), "<br>";//Output Combination $x=$last; $count++; } if($x= = 0) {//all combinations completed Break; }}Echo' Total: ',$count, "\ n";?>
Full array of PHP arrays, all combinations of elements