search
Homephp教程php手册php求数组全排列,元素所有组合的方法,php数组

php求数组全排列,元素所有组合的方法,php数组

本文实例讲述了php求数组全排列,元素所有组合的方法。分享给大家供大家参考,具体如下:

<&#63;php
$source = array('pll','我','爱','你','嘿');
sort($source); //保证初始数组是有序的
$last = count($source) - 1; //$source尾部元素下标
$x = $last;
$count = 1; //组合个数统计
echo implode(',', $source), "<br>"; //输出第一种组合
while (true) {
  $y = $x--; //相邻的两个元素
  if ($source[$x] < $source[$y]) { //如果前一个元素的值小于后一个元素的值
    $z = $last;
    while ($source[$x] > $source[$z]) { //从尾部开始,找到第一个大于 $x 元素的值
      $z--;
    }
    /* 交换 $x 和 $z 元素的值 */
    list($source[$x], $source[$z]) = array($source[$z], $source[$x]);
    /* 将 $y 之后的元素全部逆向排列 */
    for ($i = $last; $i > $y; $i--, $y++) {
      list($source[$i], $source[$y]) = array($source[$y], $source[$i]);
    }
    echo implode(',', $source), "<br>"; //输出组合
    $x = $last;
    $count++;
  }
  if ($x == 0) { //全部组合完毕
    break;
  }
}
echo 'Total: ', $count, "\n";
&#63;>

更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP数组(Array)操作技巧大全》、《PHP数学运算技巧总结》、《php正则表达式用法总结》、《PHP+ajax技巧与应用小结》、《PHP运算与运算符用法总结》、《PHP网络编程技巧总结》、《PHP基本语法入门教程》、《php日期与时间用法总结》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

希望本文所述对大家PHP程序设计有所帮助。

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool