This article mainly introduces php's method of finding all arrays and all combinations of elements, involving php's techniques for separating arrays and strings, traversing, and mathematical operations, for more information, see the example in this article. We will share this with you for your reference. The details are as follows:
<? Php $ source = array ('pll ',' I ', 'ay', 'you', 'hi'); sort ($ source ); // ensure that the initial array is ordered $ last = count ($ source)-1; // The subscript of the element at the end of $ source $ x = $ last; $ count = 1; // echo implode (',', $ source ),"
"; // Output the first combination while (true) {$ y = $ x --; // two adjacent elements, 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 from the end, find the first value greater than $ x element $ z --;} /* exchange the values of $ x and $ z elements */list ($ source [$ x], $ source [$ z]) = array ($ source [$ z], $ source [$ x]);/* sort all elements after $ y in reverse order */for ($ I = $ last; $ I> $ y; $ I --, $ y ++) {list ($ source [$ I], $ source [$ y]) = array ($ source [$ y], $ source [$ I]);} echo implode (',', $ source ),"
"; // Output combination $ x = $ last; $ count ++;} if ($ x = 0) {// all combinations are completed break ;}} echo 'total: ', $ count, "\ n";?>