简体   繁体   English

将Array值推送到其他每个Array值?

[英]Push Array value to every other Array value?

I am not so sure how to explain my problem. 我不太确定如何解释我的问题。

I have Array like this 我有像这样的数组

$foo = array{"one", "two", "three"}

and i need to push every value to other value. 我需要将每个值推到其​​他值。

need result kind like this ... 需要结果这样......

$new = array{"one two three", "one two", "one three", "two three", "one", "two", "three"}

IS that posible to make result like that ? 有可能做出那样的结果吗?

I hope somebody can help me. 我希望有人可以帮助我。

Thanks before :) 谢谢之前:)

看起来你正在寻找一个powerset,这里是这个算法的链接: http//en.wikipedia.org/wiki/Power_set

Here is a rather simplistic attempt at your problem. 这是对你的问题的一个相当简单的尝试。

<?php
function perms($array) {
    $res = array( array() );
    foreach ($array as $val) {
        $n = count($res);
        for($i=0; $i < $n; $i++) {
            $res[] = array_merge( array($val), $res[$i] );
        }
    }
    return $res;
}
$array = array("one", "two", "three");
print_r( perms($array) );

Result can be found on Ideone . 结果可以在Ideone找到 The output is in a multi-dimensional form, but I'm sure you can figure out how to flatten that. 输出是多维的形式,但我相信你可以弄清楚如何压扁它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM