简体   繁体   English

比较数组,直到有唯一的数组

[英]Comparing arrays until I have a unique array

I have the following multidimensional array called $existing_combinations 我有以下多维数组,称为$existing_combinations

Array
(
    [0] => Array
        (
            [1] => 6
            [2] => 7
            [3] => 9
        )

    [1] => Array
        (
            [1] => 1
            [2] => 21
            [3] => 9
        )

    [2] => Array
        (
            [1] => 1
            [2] => 7
            [3] => 9
        )

)

I then generate a new array ( $new_combination ) which has a combination of the same set of values. 然后,我生成一个新数组( $new_combination ),它具有一组相同值的组合。 Example: 例:

Array
(
    [1] => 6
    [2] => 21
    [3] => 9
)

I then test if $new_combination exists in $existing_combinations with the following in the hope that I will end with a unique combination in $new_combination : 然后,我测试是否$new_combination存在于$existing_combinations与希望,我将在一个独特的组合结束了以下$new_combination

foreach($existing_combinations as $key => $combination){
    while($new_combination == $combination){
        $new_combination = generateNewCombination();
    }
}

The problem is that if $new_combination matches an array that is not at index 0, then when I generate a new combination I am at risk of this matching a $combination that has already been tested against (and will not be tested again). 问题是,如果$new_combination匹配的数组不在索引0处,那么当我生成一个新的组合时,我就有这种匹配的风险,那就是已经针对该$combination进行了测试(并且将不会再次对其进行测试)。

Sorry if this is a simple one but I'm struggling to think of how I can ensure $new_combination will always end up unique. 抱歉,如果这是一个简单的$new_combination但我很难考虑如何确保$new_combination始终唯一。

Any ideas? 有任何想法吗?

Thanks 谢谢

You can use in_array in this case, because php compares arrays as value. 在这种情况下,可以使用in_array,因为php会将数组作为值进行比较。 So, the code can be: 因此,代码可以是:

while(in_array($new_combination = generateNewCombination(), $existing_combinations));

print_r($new_combination);

I wrote the below before realizing that in_array can also see if an array exists within an array. 在意识到in_array也可以查看数组中是否存在数组之前,我写了以下内容。 So you can simply do this: 因此,您可以简单地执行以下操作:

if (!in_array($new_combination, $existing_combinations)) {
    // It's unique.
}

In the below outdated bit, see the note on using sort , if a different sequence of the same numbers isn't considered unique for your purposes. 如果您认为不同的相同数字序列不是唯一的,请在下面的过时位中查看有关使用sort的注释。


[ For Entertainment ] [ 为了娱乐 ]

May not be the most elegant way around, but I would simply do this to keep it simple: 可能不是最优雅的方法,但我只是为了简化起见:

$combo_hashes = [];

// Implode the existing combos into strings.
foreach($existing as $vals) {
    $combo_hashes[] = implode(':', $vals);
}

Then, all you need to check with your new combo is: 然后,您需要检查的新组合是:

// Check if the "hash"-string already exists.

if (!in_array( implode(':', $new_combo), $combo_hashes)) {
    // ... and we're unique.
}

This presumes that you consider [1,3,2] different from [2,1,3] . 假设您认为[1,3,2][2,1,3]不同。 If they are equivalent (I don't know what your use case is), you should sort($vals); 如果它们相等(我不知道您的用例是什么),则应该进行sort($vals); before you generate the check-strings. 在生成检查字符串之前。

合并所有第二级数组,然后运行array_unique()摆脱重复的值。

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

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