Jump to content

Recommended Posts

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 by AlecGuinn
Link to comment
https://forums.phpfreaks.com/topic/314593-get_combinations-w-unique-values/
Share on other sites

  • AlecGuinn changed the title to get_combinations w/ unique values

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
        )

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.