Hello! Faced such a problem. I need to dynamically create an associative array, receiving from the user 1 parameter, a number, or rather the number of device classes. 3 class device. Each class can be implemented on a processor or on a chip, i.e. the value of class n can be 1 or 0. I need to get an array containing all possible combinations of the numbers 1 and 0 "long" n. I get the number n as an input, then I get the total number of possible combinations, 2 to the power n. I run a cycle in which I generate an element of the i-th row of the array (the 2-dimensional array should be)
$arr[$i]["Class".$j.""] = rand(0,1);
The variable $ j in my case takes values ββfrom 1 to n inclusive. Those. OK class in iq line. In parallel with this, I write down the string variable "Key".
$key .= $arr[$i]["Class".$j.""];
It will be required for comparison with an existing entry in the array. Those. for example, I got such a combination of classes {1; 0; 1} and the key will be equal to string (3) "101", and I will check it in a loop with other keys of the array and if it is NOT a match, write "simplex" in array At first, the logic seemed correct to me, but then I began to doubt. Because somehow I do not write correctly at the beginning of the classes, collect the key, compare the key, did not match - I write down, I match - I delete the classes that I wrote down before the comparison. Maybe someone has an idea for the implementation of such an algorithm?
The input is the number n (the number of classes), the output is such a matrix:
class1|class2|class3|classn|Key| |1|0|1|..|n|101| ------------ |1|0|0|..|n|100| ------------ |0|0|0|..|n|000| ------------ |0|1|0|..|n|010|
.... etc.
however, strings or keys (since keys are "alias" strings) are unique. Thank you in advance!