当前位置: 首页 > 新闻动态 > 软件编程

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

作者:用户投稿 浏览: 发布日期:2026-01-16
[导读]:要求实现一个7个元素的数组,取出这个数组中的5个元素,生成新的数组,这些数组是唯一的

复制代码 代码如下:
<?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>";  

免责声明:转载请注明出处:http://shjed.com/news/265539.html

扫一扫高效沟通

多一份参考总有益处

免费领取网站策划SEO优化策划方案

请填写下方表单,我们会尽快与您联系
感谢您的咨询,我们会尽快给您回复!