Home  >  Article  >  Backend Development  >  问下怎么生成所有的不重复的数组

问下怎么生成所有的不重复的数组

WBOY
WBOYOriginal
2016-06-23 13:45:45883browse

比如 ab
生成
ab
ba
比如  a b c
生成所有的
abc
acb
bac
bca
cab
cba


回复讨论(解决方案)

$str    = 'abc';$res    = Arrangement( $str );print_r($res);function Arrangement($arr = array(), $res = '') {  if(! is_array($arr) ) $arr = str_split($arr);  if(empty($arr)) $array[] = $res;  else foreach($arr AS $k => $v) {    unset($arr[$k]);    foreach( Arrangement($arr, $res . $v) AS $t) $array[] = $t;    $arr[$k]    = $v;  }  return  $array;}
Array(    [0] => abc    [1] => acb    [2] => bca    [3] => bac    [4] => cab    [5] => cba)

非常感谢,是我要的效果

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