AlecGuinn Posted March 11, 2022 Share Posted March 11, 2022 (edited) Hey ! I have found this get_combinations function which kinda seems to do what i need. BUT I need the results to only have unique values "[item1] => A [item2] => C [item3] => B" is OK, but "[item1] => A [item2] => A [item3] => B" is NOT. How can i get rid of this non unique combination ? Thanks function get_combinations($arrays) { $result = array(array()); foreach ($arrays as $property => $property_values) { $tmp = array(); foreach ($result as $result_item) { foreach ($property_values as $property_value) { $tmp[] = array_merge($result_item, array($property => $property_value)); } } $result = $tmp; } return $result; } $combinations = get_combinations( array( 'item1' => array('A', 'B'), 'item2' => array('A', 'D', 'E', 'F'), 'item3' => array('B', 'E', 'F'), ) ); var_dump($combinations); array(24) { [0]=> array(3) { ["item1"]=> string(1) "A" ["item2"]=> string(1) "A" ["item3"]=> string(1) "B" } [1]=> array(3) { ["item1"]=> string(1) "A" ["item2"]=> string(1) "A" ["item3"]=> string(1) "E" } [2]=> array(3) { ["item1"]=> string(1) "A" ["item2"]=> string(1) "A" ["item3"]=> string(1) "F" } [3]=> array(3) { ["item1"]=> string(1) "A" ["item2"]=> string(1) "D" ["item3"]=> string(1) "B" } [4]=> array(3) { ["item1"]=> string(1) "A" ["item2"]=> string(1) "D" ["item3"]=> string(1) "E" } [5]=> array(3) { ["item1"]=> string(1) "A" ["item2"]=> string(1) "D" ["item3"]=> string(1) "F" } [6]=> array(3) { ["item1"]=> string(1) "A" ["item2"]=> string(1) "E" ["item3"]=> string(1) "B" } [7]=> array(3) { ["item1"]=> string(1) "A" ["item2"]=> string(1) "E" ["item3"]=> string(1) "E" } [8]=> array(3) { ["item1"]=> string(1) "A" ["item2"]=> string(1) "E" ["item3"]=> string(1) "F" } [9]=> array(3) { ["item1"]=> string(1) "A" ["item2"]=> string(1) "F" ["item3"]=> string(1) "B" } [10]=> array(3) { ["item1"]=> string(1) "A" ["item2"]=> string(1) "F" ["item3"]=> string(1) "E" } [11]=> array(3) { ["item1"]=> string(1) "A" ["item2"]=> string(1) "F" ["item3"]=> string(1) "F" } [12]=> array(3) { ["item1"]=> string(1) "B" ["item2"]=> string(1) "A" ["item3"]=> string(1) "B" } [13]=> array(3) { ["item1"]=> string(1) "B" ["item2"]=> string(1) "A" ["item3"]=> string(1) "E" } [14]=> array(3) { ["item1"]=> string(1) "B" ["item2"]=> string(1) "A" ["item3"]=> string(1) "F" } [15]=> array(3) { ["item1"]=> string(1) "B" ["item2"]=> string(1) "D" ["item3"]=> string(1) "B" } [16]=> array(3) { ["item1"]=> string(1) "B" ["item2"]=> string(1) "D" ["item3"]=> string(1) "E" } [17]=> array(3) { ["item1"]=> string(1) "B" ["item2"]=> string(1) "D" ["item3"]=> string(1) "F" } [18]=> array(3) { ["item1"]=> string(1) "B" ["item2"]=> string(1) "E" ["item3"]=> string(1) "B" } [19]=> array(3) { ["item1"]=> string(1) "B" ["item2"]=> string(1) "E" ["item3"]=> string(1) "E" } [20]=> array(3) { ["item1"]=> string(1) "B" ["item2"]=> string(1) "E" ["item3"]=> string(1) "F" } [21]=> array(3) { ["item1"]=> string(1) "B" ["item2"]=> string(1) "F" ["item3"]=> string(1) "B" } [22]=> array(3) { ["item1"]=> string(1) "B" ["item2"]=> string(1) "F" ["item3"]=> string(1) "E" } [23]=> array(3) { ["item1"]=> string(1) "B" ["item2"]=> string(1) "F" ["item3"]=> string(1) "F" } } int(1) Edited March 11, 2022 by AlecGuinn Quote Link to comment https://forums.phpfreaks.com/topic/314593-get_combinations-w-unique-values/ Share on other sites More sharing options...
Barand Posted March 11, 2022 Share Posted March 11, 2022 You could add these two lines foreach ($combinations as $k => &$a) sort($a); $combinations = array_values(array_unique($combinations, SORT_REGULAR)); Quote Link to comment https://forums.phpfreaks.com/topic/314593-get_combinations-w-unique-values/#findComment-1594367 Share on other sites More sharing options...
AlecGuinn Posted March 11, 2022 Author Share Posted March 11, 2022 Thanks for your anwser ! I dont understand what this two lines should do. I would like to keep keys (item1,item2...) but I still have non unique values. Like the 2 first combinations i still have 2 A and 3 B and 2 A and 2 B. A,B,C are users. Each users should have only 1 item. [0] => Array ( [0] => A [1] => A [2] => B ) [1] => Array ( [0] => A [1] => A [2] => E ) I would like to have something like that: [0] => Array ( [item1] => A [item2] => C [item3] => B ) [1] => Array ( [item1] => B [item2] => A [item3] => D ) Quote Link to comment https://forums.phpfreaks.com/topic/314593-get_combinations-w-unique-values/#findComment-1594368 Share on other sites More sharing options...
Barand Posted March 11, 2022 Share Posted March 11, 2022 33 minutes ago, AlecGuinn said: I dont understand what this two lines should do. This is when reading the manual comes in useful php.net/array_values php.net/array_unique php.net/sort Quote Link to comment https://forums.phpfreaks.com/topic/314593-get_combinations-w-unique-values/#findComment-1594369 Share on other sites More sharing options...
Barand Posted March 11, 2022 Share Posted March 11, 2022 Plan B Use this line instead of the two I gave earlier $combinations = array_values(array_filter( $combinations, function($v) { return count($v) == count(array_unique($v)); })); Quote Link to comment https://forums.phpfreaks.com/topic/314593-get_combinations-w-unique-values/#findComment-1594370 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.