Home > Backend Development > PHP Tutorial > 问下如何生成所有的不重复的数组

问下如何生成所有的不重复的数组

WBOY
Release: 2016-06-13 12:09:39
Original
957 people have browsed it

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

本帖最后由 wxinlin 于 2014-11-12 19:06:14 编辑 比如 ab
生成
ab
ba
比如  a b c
生成所有的
abc
acb
bac
bca
cab
cba

------解决思路----------------------
$str    = 'abc';<br />$res    = Arrangement( $str );<br />print_r($res);<br /><br />function Arrangement($arr = array(), $res = '') {<br />  if(! is_array($arr) ) $arr = str_split($arr);<br />  if(empty($arr)) $array[] = $res;<br />  else foreach($arr AS $k => $v) {<br />    unset($arr[$k]);<br />    foreach( Arrangement($arr, $res . $v) AS $t) $array[] = $t;<br />    $arr[$k]    = $v;<br />  }<br />  return  $array;<br />}
Copy after login
Array<br />(<br />    [0] => abc<br />    [1] => acb<br />    [2] => bca<br />    [3] => bac<br />    [4] => cab<br />    [5] => cba<br />)<br /><br />
Copy after login
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template