首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >获得多个数组的唯一变体

获得多个数组的唯一变体
EN

Stack Overflow用户
提问于 2017-05-18 08:01:09
回答 2查看 651关注 0票数 0

我有一个数组,其中有不同的项目,如衬衫,鞋子,领带,夹克等。这些项目中的每一个可以有多个设计代表他们的ids。

代码语言:javascript
复制
$variations = array(
    'shirts' => array('2'),
    'shoes' => array('7', '3'),
    'jackets' => array('1', '5')
);

现在,我们正在寻找一种有效的方法来创建所有这些不同的变体。

代码语言:javascript
复制
## Required result ##
$result = array(
   array('2','7','1'),
   array('2', '7', '5'),
   array('2','3','1'),
   array('2','3','5')
);

如能提供任何帮助,将不胜感激:)

编辑:我们的当前功能

代码语言:javascript
复制
function cartesian($input) {
    $result = array();

    while (list($key, $values) = each($input)) {
        // If a sub-array is empty, it doesn't affect the cartesian product
        if (empty($values)) {
            continue;
        }

        // Seeding the product array with the values from the first sub-array
        if (empty($result)) {
            foreach($values as $value) {
                $result[] = array($key => $value);
            }
        }
        else {
            // Second and subsequent input sub-arrays work like this:
            //   1. In each existing array inside $product, add an item with
            //      key == $key and value == first item in input sub-array
            //   2. Then, for each remaining item in current input sub-array,
            //      add a copy of each existing array inside $product with
            //      key == $key and value == first item of input sub-array

            // Store all items to be added to $product here; adding them
            // inside the foreach will result in an infinite loop
            $append = array();

            foreach($result as &$product) {
                // Do step 1 above. array_shift is not the most efficient, but
                // it allows us to iterate over the rest of the items with a
                // simple foreach, making the code short and easy to read.
                $product[$key] = array_shift($values);

                // $product is by reference (that's why the key we added above
                // will appear in the end result), so make a copy of it here
                $copy = $product;

                // Do step 2 above.
                foreach($values as $item) {
                    $copy[$key] = $item;
                    $append[] = $copy;
                }

                // Undo the side effecst of array_shift
                array_unshift($values, $product[$key]);
            }

            // Out of the foreach, we can add to $results now
            $result = array_merge($result, $append);
        }
    }

    return $result;
}
EN

回答 2

Stack Overflow用户

发布于 2017-05-18 10:03:52

虽然我同意您的问题下面的评论,但为了好玩,我实现了基于生成器的解决方案,这样我就可以分享它了:

代码语言:javascript
复制
$variations = array(
    'shirts' => array('2'),
    'shoes' => array('7', '3'),
    'jackets' => array('1', '5')
);

var_dump(iterator_to_array(cartesian($variations), false));

function cartesian($lists, $product = []) 
{
        if (empty($product)) {
                // first run, reverse array for array_pop and remove empty lists
                $lists = array_reverse(array_filter($lists, 'count'));
        }
        $curr = array_pop($lists);
        foreach ($curr as $c) {
                if (empty($lists)) {
                        yield array_merge($product, [$c]);
                } else {
                        yield from cartesian($lists, array_merge($product, [$c]));
                }
        }
}
票数 0
EN

Stack Overflow用户

发布于 2017-07-19 13:56:50

我就是这样做的

代码语言:javascript
复制
$parameters = array(
    'shirts' => array('2'),
    'shoes' => array('7', '3'),
    'jackets' => array('1', '5')
);    
$arPhrases = $parameters[0];

for ($i = 1; $i < count($parameters); $i++) {
    $notFullCount = count($arPhrases);
    foreach ($arPhrases as $phrase) {
        foreach ($parameters[$i] as $newPart) {
            $arPhrases[] = $phrase." ".$newPart;
        }
    }
    $arPhrases = array_slice($arPhrases, $notFullCount);
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44041921

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档