本文转载:http://blog.lihuanliang.com/?p=124 /** * 任意组合 * @param $arr 数组 * @param $count 数组中任意组合的个数(如:array('a','b','c'),传2则为数组*中任意两个字母的组合,传3则为三个字母的任意组合,超过数组元素个数或者数量小于等于0则返回空数组) * @return array */ function ArbitraryCombination($arr, $count) { $result = array(); if ($count <= 0 || $count > count($arr)) { return $result; } for ($i=0; $i<count($arr); $i++) { $tem1_arr = $arr; $result1 = array_splice($tem1_arr, $i, 1); if ($count == 1) { $result[] = $result1; } else { $tem2_arr = ArbitraryCombination($tem1_arr, $count-1); foreach ($tem2_arr as $v) { $result[] = array_merge($result1, $v); } } } return $result; } $arr = array("a", "b", "c"); $result = ArbitraryCombination($arr, count($arr)); print_r($result);
PHP 任意组合
最新推荐文章于 2021-03-26 01:54:26 发布