Home >php教程 >php手册 >php生成数组的使用示例 php全组合算法

php生成数组的使用示例 php全组合算法

WBOY
WBOYOriginal
2016-06-06 20:25:341823browse

要求实现一个7个元素的数组,取出这个数组中的5个元素,生成新的数组,这些数组是唯一的

复制代码 代码如下:


$arr = array(1,2,3,4,5);
/*
@ res  组装好的每一注的每个元素
@ $a 临时数组
@ $index 数组索引
@ $arr 请求的数组
@ $b 最后要的结果
*/
$a = array();
$b = array();
$total = 0;
format($a,0,$arr);
function format($res,$index,$arr){
 global $total;
 global $b;
 $new_arr = $res;
 $n_arr = array();
 if($index == count($arr)){
  if(count($new_arr)==5){
   $b[]  = $new_arr;
   echo "

";<br>   $total++; <br>  }  <br>  return;<br> }<br> format($new_arr,$index+1,$arr);<br> $new_arr[] = $arr[$index];<br> format($new_arr,$index+1,$arr);<br>}
<p>echo "</p><pre class="brush:php;toolbar:false">";<br>print_r($b);<br>echo "
";  
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