search
Homephp教程php手册把$input_arr进行组合操作,并返回一个包含所有组合的数组

数组

/**
 *描述:把$input_arr进行组合操作,并返回一个包含所有组合的数组
 *参数:$input_arr 操作对象,$delimit 组合的分割符号,$n 每个组合的数组单元数
 *作者:surfchen@gmail.com
 *版权:任意复制,修改所有内容.
**/
function getSingleComb($input_arr,$delimit,$n)
{
 if ($n==1)
 {
  return $input_arr;
 }
 $c_num=getCNum($n,count($input_arr));
 $new_arr=array();
 while (1)
 {
  $r_arr=array_rand($input_arr,$n);
  asort($r_arr);
  foreach ($r_arr as $value)
  {
   $tmp_str[]=$input_arr[$value];
  }
  $new_str=implode($delimit,$tmp_str);
  unset($tmp_str);
  if (!in_array($new_str,$new_arr))
  {
   $new_arr[]=$new_str;
  }
  if (count($new_arr)>=$c_num)
  {
   break;
  }
 }
 return $new_arr;
 
}
/**
 *描述:获得组合(请参阅初等数学的排列与组合)个数
 *参数:$m 组合的单元数,$n 单元总数
 *作者:surfchen@gmail.com
 *版权:任意复制,修改所有内容.
**/
function getCNum($m,$n)
{
 $store_m=$m;
 $store_n=$n;
 $dividend=1;
 for ($m;$m>1;$m--)
 {
  $dividend*=$m;
 }
 $divisor=1;
 for ($n;$n>=($store_n-$store_m+1);$n--)
 {
  $divisor*=$n;
 }
 return $divisor/$dividend;
}
/**
 *描述:把$input_arr进行组合操作,并返回一个包含所有组合的数组
 *参数:$input_arr 操作对象,$delimit 组合的分割符号,$n_arr 为一个数组,其值范围必须在从1到count($input_arr)之间
 *作者:surfchen@gmail.com
 *版权:任意复制,修改所有内容.
**/
function getMultiComb($input_arr,$delimit,$n_arr)
{
 $return_arr=array();
 foreach ($n_arr as $value)
 {
  $return_arr=array_merge($return_arr,getSingleComb($input_arr,$delimit,$value));
 }
 return $return_arr;
}
$input_arr=explode(" ",'sina sohu yahoo baidu 163');
print_r(getMultiComb($input_arr," ",array(1,2,3,4)));
?>



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

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor