您的位置:首页 > 编程语言 > PHP开发

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

2018-10-12 13:50 615 查看


<?php
$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 "<pre>";
   $total++; 
  }  
  return;
 }
 format($new_arr,$index+1,$arr);
 $new_arr[] = $arr[$index];
 format($new_arr,$index+1,$arr);
}

echo "<pre>";
print_r($b);
echo "</pre>";  

您可能感兴趣的文章:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: