PHP to find the array of all permutations, elements of all combinations of methods _php tips

Source: Internet
Author: User
Tags php database php regular expression

The example of this article describes the method of PHP to find all array of arrays, all combinations of elements. Share to everyone for your reference, specific as follows:

<?php
$source = Array (' PLL ', ' I ', ' love ', ' You ', ' Hey ');
Sort ($source); Ensure that the initial array is ordered
$last = count ($source)-1;//$source tail element subscript
$x = $last;
$count = 1; Number of combinations the
echo implode (', ', $source), "<br>";//output The first combination while
(true) {
  $y = $x-;//Adjacent two elements
  if ($ source[$x] < $source [$y]) {//If the value of the previous element is less than the value of the latter element
    $z = $last;
    while ($source [$x] > $source [$z]) {//start from the tail, find the first value greater than the $x element
      $z-;
    }
    /* Exchange $x and $z the value of the element/
    list ($source [$x], $source [$z]) = Array ($source [$z], $source [$x]);
    /* The elements after the $y are all in reverse order
    ($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 complete break
    ;
  }
}
Echo ' total: ', $count, "\ n";
? >

For more information on PHP related content readers can view the site topics: "PHP array" Operation Techniques Encyclopedia, "PHP Mathematical Calculation Skills Summary", "PHP Regular Expression Usage Summary", "Php+ajax Tips and Application Summary", "PHP operation and operator Usage Summary", " PHP Network Programming Skills Summary, "Introduction to PHP Basic Grammar", "PHP date and Time usage summary", "PHP object-oriented Programming Introductory Course", "PHP string (String) Usage Summary", "Php+mysql database Operation Tutorial" and " A summary of common PHP database operations tips

I hope this article will help you with the PHP program design.

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.