【问题标题】:PHP arrays - unique combinations out of 3 arraysPHP 数组 - 3 个数组中的独特组合
【发布时间】:2018-04-20 11:51:00
【问题描述】:

我什至不确定如何解决这个问题,所以我只是陈述问题。非常感谢任何帮助。

所有可能的值都有这个数组($colors):

$colors = array ('red','blue','green','yellow');

然后有一个包含所有可能值的数组 ($boxes) - 由与 $colors 相同数量的值组成:

$boxes = array ('circular','squared','hexagonal','triangular');

第三个数组定义了唯一组合的约束:

$possible_combos = array  
(  
array('circular','red','blue'),  
array('squared','red','green'),
array('hexagonal','blue','yellow'),
array('triangular','red','green')
);

问题:我如何获得一个带有键=>值组合的新数组 $result,只是每个框都被分配了一个唯一的颜色可能的颜色集

所以一个有效的 $result 数组应该是:

Array ( [circular] => red 
        [squared]  => blue 
        [hexagonal]=> yellow
        [triangular] => green
      ) 

注意:如果您按顺序遍历,“红色”、“蓝色”、“绿色”可能会被分配给前三个“盒子”,而第四个盒子可能没有任何选择(因为“黄色”是不允许的分配给它。
我在想,首先处理最少出现的“颜色”,但真的不确定如何在语法上处理它。

再次感谢您提前调查!即使是一些关于如何解决问题的帮助也会很好。

以下代码也没有产生正确的输出:

foreach ($colors as $c => $color) {
    foreach ($boxes as $b => $box) {
        for ($i=0; $i < count($colors) ; $i++) {
            if(in_array($possible_combos[$i][1],$colors) && !in_array($possible_combos[$i][1], $result))
            {
                $result[$box] = $possible_combos[$i][1];
                unset($colors[$c]);
                break;
            }
            else if(in_array($possible_combos[$i][2],$colors) && !in_array($possible_combos[$i][2], $result))
            {
                $result[$box] = $possible_combos[$i][2];
                unset($colors[$c]);
                break;
            }
        }
    }
}

【问题讨论】:

  • triangular =&gt; yellow 不是输出错误吗?
  • 看不懂....具体输出应该是什么?
  • 你只需要看看是否取值然后选择另一个,如果两者都取则抛出异常。
  • @mickmackusa 我不是要你为我写代码。我需要知道解决方案的伪代码类型,以便我可以使用这个简化版本来解决实际问题。我是一名初学者程序员,无论如何我更愿意自己编写代码。只需要一些指针。
  • 现在我看到每种颜色只能使用一次——我以前没有看到过。我已投票重新开放并删除了我的反对票和赞成票。

标签: php arrays unique combinations nested-loops


【解决方案1】:

我在想,首先处理最少出现的“颜色”,但真的不确定如何在语法上处理它。

考虑到您只有一种可以共享多种颜色中的一种的盒子类型,这是一个很好的直觉。因为您的约束资源是颜色,所以我认为首先按它们对规则进行排序然后您可以按稀缺性进行分配是有意义的。

https://3v4l.org/u5pBK

$colors = array ('red','blue','green','yellow');

$boxes = array ('circular','squared','hexagonal','triangular');

$possible_combos = array
(
    array('circular','red','blue'),
    array('squared','red','green'),
    array('hexagonal','blue','yellow'),
    array('triangular','red','green')
);

// collect constraints ordered by rarest
foreach ($possible_combos as $constraint) {
    $box = array_shift($constraint);
    foreach ($constraint as $color) {
        $constraints[$color] []= $box;
    }
}

// assign rarest first to last
asort($constraints);
foreach ($constraints as $color => $allowedBoxes) {
    foreach ($allowedBoxes as $box) {
        $key = array_search($box, $boxes);
        // if we have a match, then remove it from the collection
        if ($key !== false) {
            $result[$box] = $color;
            unset($boxes[$key]);
            continue 2;
        }
    }
}

print_r($result);
Array
(
    [hexagonal] => yellow
    [circular] => blue
    [squared] => green
    [triangular] => red
)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-26
    • 1970-01-01
    • 2013-08-28
    • 1970-01-01
    • 2017-12-23
    • 2016-03-08
    • 1970-01-01
    相关资源
    最近更新 更多