Array <?php
/**
* Description: Combine the $input_arr and return an array containing all the combinations
* Parameter: $input _arr Action object, $delimit the combined split symbol, $n the number of array cells for each combination
* Author: surfchen@gmail.com
* Copyright: Any copy, modify all content.
**/
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;
}
/**
* Description: Get a combination (see arrangement and combination of elementary mathematics)
* Parameters: $m number of units combined, $n total number of units
* Author: surfchen@gmail.com
* Copyright: Any copy, modify all content.
**/
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;
}
/**
* Description: Combine the $input_arr and return an array containing all the combinations
* Parameter: $input _arr Action object, $delimit a combined split symbol $n _arr as an array with a range of values between 1 and count ($input _arr)
* Author: surfchen@gmail.com
* Copyright: Any copy, modify all content.
**/
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)));
?>
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service