4

What is the simplest way to convert this PHP array

$a = array('A' => array(1, 2),
           'B' => array(3, 4),
           'C' => array(5));

into this:

$result = array(
    array('A' => 1, 'B' => 3, 'C' => 5),
    array('A' => 1, 'B' => 4, 'C' => 5),
    array('A' => 2, 'B' => 3, 'C' => 5),
    array('A' => 2, 'B' => 4, 'C' => 5),
);

$a may have many different keys I don't know during development time. So I need to generate all combinations in given array.

UPDATE:

I have to generate URLs based in incoming array. So I don't know how many parameters I will get during development time. I have only array of parameters, it can be e.g.

$a = array('A' => array(5,3, 1));

result will be:

$result = array(
    array('A' => 5),
    array('A' => 3),
    array('A' => 1));

or

$a = array('X' => array(5), 'D' => array(4, 7));

result will be:

$result = array(
    array('X' => 5, 'D' => 4),
    array('X' => 5, 'D' => 7));
4
  • Have you tried anything yourself? Also, could we see more of an extended example to better understand the pattern? Commented Jul 30, 2013 at 8:50
  • I would assume, that there is no plug-in solution, you just have to do it yourself. Commented Jul 30, 2013 at 8:51
  • 1
    possible duplicate of How to generate in PHP all combinations of items in multiple arrays Commented Jul 30, 2013 at 8:56
  • Please, see updated question with more example. Yes, I tried by myself to do it with nested foreach cycles, but nothing happened. Commented Jul 30, 2013 at 8:57

2 Answers 2

6

Like this one:

$a = array('A' => array(1, 2),
           'B' => array(3, 4),
           'C' => array(5));

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;
}

Output

var_dump(get_combinations($a));

array (size=4)
  0 => 
    array (size=3)
      'A' => int 1
      'B' => int 3
      'C' => int 5
  1 => 
    array (size=3)
      'A' => int 1
      'B' => int 4
      'C' => int 5
  2 => 
    array (size=3)
      'A' => int 2
      'B' => int 3
      'C' => int 5
  3 => 
    array (size=3)
      'A' => int 2
      'B' => int 4
      'C' => int 5
Sign up to request clarification or add additional context in comments.

Comments

0

You can use this function for this request:

function pc_array_power_set($array) {
    // initialize by adding the empty set
    $results = array(array( ));

    foreach ($array as $element)
        foreach ($results as $combination)
            array_push($results, array_merge(array($element), $combination));

    return $results;
}

Usage:

$set = array('A', 'B', 'C');
$power_set = pc_array_power_set($set);

Output:

array( );
array('A');
array('B');
array('C');
array('A', 'B');
array('A', 'C');
array('B', 'C');
array('A', 'B', 'C');

Resource: http://docstore.mik.ua/orelly/webprog/pcook/ch04_25.htm

Comments

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.