Requires the combination of the two-dimensional array of duplicate keys and values are removed, such as array (' a ' = ' = ' B '), Array (' B ' = ' a ')); For the same combination, keep only the array (' a ' = ' = ' B '). Note that combinations and permutations are different, (a, B) and (b,a) are the same combinations, but they are different permutations. Tip: 1, the two-dimensional array into a one-dimensional array, in particular, the two-dimensional array of keys and values are combined, all stored in a one-dimensional array, such as Array (' a ' + ' B '), we convert the key and value into a string like a-c and then stored in a one-dimensional array, plus '-' , you can restore the original key-value pair. 2, after the conversion, the repetition of the string is removed, in the example is ' b-d ', it repeated 2 times, remove one. To compare again. Our comparison is like this, such as the key value string stored in a one-dimensional array ' A-B ', we convert it to ' b-a ', and then search in a one-dimensional array, if the array does not have a string like ' b-a ', then the "a-B" does not duplicate the combination, then the ' A-B ' split, Stored in a two-dimensional array as an initial array, if a string with a shape like ' b-a ' is present in the array, and after the ' A-B ' string, then the ' A-B ' string has a duplicate combination, but we still split it and store it in a two-dimensional array. This is because repeated combinations of ' b-a ' are located after ' A-a ', and when we loop through the string ' b-a ', we get the string ' b-a ' There is also a duplicate string combination of ' A-B ', but before ' A-B ' again ' b-a ', we have stored it, so ' b-a ' is no longer stored, This also achieves the goal of removing duplicate combinations. A comparison of the position of a repeating string is done by its corresponding key name in a one-dimensional array. Full code:
' B '), Array (' a ' = = ' C '), Array (' a ' = ' = ' d '), Array (' b ' = = ' a '), array (' b ' = = ' C '), Array (' b ' = = ' d '), Array (' B ' = > ' d '), Array (' c ' = = ' a '), array (' d ' = = ' a '), array (' d ' and ' e '));//Loop two-dimensional array, get one-dimensional array foreach ($array as $key =>$ Value) {foreach ($value as $key 2=> $value 2) {$new []= $key 2. '-'. $value 2;}} Remove duplicates in a one-dimensional array $new=array_unique ($new);//loop one-dimensional array foreach ($new as $key + $value) {$varr =explode ('-', $value);//Split string $ Nval= $varr [1]. ' -'. $varr [0];//recombination string $skey=array_search ($nval, $new);//Search in the array, string, if there is a return key name, did not return false//note here to flase comparison must use = = =, but not with = =, Because of the special key 0, if the comparison with = = will also be considered Falseif ($skey ===false| | $skey > $key) {//If there are no repeating combinations or repeating combinations after the current combination $res[][$varr [0]]= $varr [1];//}}//The final result Print_r ($res) by the initial array of key-value pairs;? >
|