0

I have the following multidimensional array called $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. 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:

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).

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.

Any ideas?

Thanks

3 Answers 3

2

You can use in_array in this case, because php compares arrays as value. So, the code can be:

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

print_r($new_combination);
Sign up to request clarification or add additional context in comments.

3 Comments

That is about as compact as it gets for generating and checking for a new unique array in one shot. If we're generating two-digit triplets and presumably appending the new combo to the existing array, the while will result in an infinite loop after the 1000000 iterations have exhausted all unique combinations. May never become an issue in practice, but then again I have no idea what the use case here is.
the code you wrote shows only condition to test new value but not algorithm of make. where is else?
I've only written about verifying if the new combination is unique.
1

I wrote the below before realizing that in_array can also see if an array exists within an 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.


[ 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]. If they are equivalent (I don't know what your use case is), you should sort($vals); before you generate the check-strings.

1 Comment

At first I suspected but didn't bother, then I read and I understood, then I went ahead to test to be sure, and then I realized. :D $a1 = []; $a1[] = [1,2,3]; $a2 = [1,3,2]; if (in_array($a2, $a1)) { echo 'not unique'; } else { echo 'is unique'; }
0

Merge all the second level arrays and run array_unique() to get rid of the duplicate values.

2 Comments

Can you show me an example of how I would do this please?
He's asking about combinations, ie. the triplets. Doing what you suggest would not help in creating new unique triplets that may have some values of the old sets.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.