栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

获取PHP数组的所有排列?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

获取PHP数组的所有排列?

function pc_permute($items, $perms = array()) {
if (empty($items)) {
echo join(‘ ‘, $perms) . “
“;
} else {
for ($i = count($items) - 1; $i >= 0; –$i) {
$newitems = $items;
$newperms = $perms;
list($foo) = array_splice($newitems, $i, 1);
array_unshift($newperms, $foo);
pc_permute($newitems, $newperms);
}
}
}


$arr = array('peter', 'paul', 'mary');pc_permute($arr);

要么

function pc_next_permutation($p, $size) {    // slide down the array looking for where we're smaller than the next guy    for ($i = $size - 1; $p[$i] >= $p[$i+1]; --$i) { }    // if this doesn't occur, we've finished our permutations    // the array is reversed: (1, 2, 3, 4) => (4, 3, 2, 1)    if ($i == -1) { return false; }    // slide down the array looking for a bigger number than what we found before    for ($j = $size; $p[$j] <= $p[$i]; --$j) { }    // swap them    $tmp = $p[$i]; $p[$i] = $p[$j]; $p[$j] = $tmp;    // now reverse the elements in between by swapping the ends    for (++$i, $j = $size; $i < $j; ++$i, --$j) {         $tmp = $p[$i]; $p[$i] = $p[$j]; $p[$j] = $tmp;    }    return $p;}$set = split(' ', 'she sells seashells'); // like array('she', 'sells', 'seashells')$size = count($set) - 1;$perm = range(0, $size);$j = 0;do {      foreach ($perm as $i) { $perms[$j][] = $set[$i]; }} while ($perm = pc_next_permutation($perm, $size) and ++$j);foreach ($perms as $p) {    print join(' ', $p) . "n";}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/394136.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号