Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/264.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP数组-3个数组中的唯一组合_Php_Arrays_Unique_Combinations_Nested Loops - Fatal编程技术网

PHP数组-3个数组中的唯一组合

PHP数组-3个数组中的唯一组合,php,arrays,unique,combinations,nested-loops,Php,Arrays,Unique,Combinations,Nested Loops,我甚至不知道如何解决这个问题,所以我只是陈述问题。非常感谢您的帮助 有一个包含所有可能值的数组($colors): $colors = array ('red','blue','green','yellow'); 然后是一个包含所有可能值的数组($box)——由与$colors数量相等的值组成: $boxes = array ('circular','squared','hexagonal','triangular'); 第三个数组定义了进行唯一组合时的约束: $possible_combo

我甚至不知道如何解决这个问题,所以我只是陈述问题。非常感谢您的帮助

有一个包含所有可能值的数组($colors):

$colors = array ('red','blue','green','yellow');
然后是一个包含所有可能值的数组($box)——由与$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')
);
问题:我如何使用key=>值组合获得一个新的数组$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;
            }
        }
    }
}
foreach($c=>color的颜色){
foreach($b=>$box的方框){
对于($i=0;$i
我在想,先处理出现最少的“颜色”,但真的不确定如何在语法上处理它

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


triangal=>yellow
不是错误的输出吗?不明白…应该精确输出什么?你只需要看看是否取了值,然后选择另一个值,如果两者都取了,然后抛出一个异常。@mickmackusa我不是要你为我写代码。我需要知道解决方案的伪代码类型,这样我就可以解决实际的p使用这个简化版本有问题。我是一个初学者,我更喜欢自己编写代码。只需要一些指针。现在我看到每种颜色只能使用一次——我以前没有看到过。我已经投票重新打开并删除了我的否决票和否决票。
$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
)