Php: how to arrange all arrays and combinations of elements _ php tips-php Tutorial

Source: Internet
Author: User
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";?>

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.