简体   繁体   中英

Multiply two arrays with different length in PHP?

I have a dynamic array:

$variants = [
    'color' => ['Blue', 'Red', 'Pink'],
    'size' => ['X', 'S'],
    ... (maybe more elements like above or not)
];

I expect this:

$result = [
    0 => ['color' => 'Blue', 'size' => 'X'],
    1 => ['color' => 'Blue', 'size' => 'S'],
    2 => ['color' => 'Red', 'size' => 'X'],
    3 => ['color' => 'Red', 'size' => 'S'],
    4 => ['color' => 'Pink', 'size' => 'X'],
    5 => ['color' => 'Pink', 'size' => 'S']
];

Result is the multiplying of all array lengths. I've searched but not found the solution yet. Hope anyone could help. Many thanks!

This is a bit tricky, you need a preparation and an aggregation step for this. But it is possible without falling back to recursion:

<?php
$variants = [
    'color' => ['Blue', 'Red', 'Pink'],
    'size' => ['X', 'S'],
    'weight' => [150, 250]
];

$result = [];
foreach ($variants as $key => $set) {
    foreach ($set as $entry) {
        $result[] = [$key => $entry];
    }
    break;
}
array_shift($variants);

foreach($variants as $setKey => $set) {
    $buffer = $result;
    $result = [];

    foreach ($set as $entry) {

        foreach ($buffer as $buf) {
            $result[] = array_merge($buf, [$setKey => $entry]);
        }
    }
}

var_dump($result);

The output of that is:

array(12) {
  [0]=>
  array(3) {
    ["color"]=>
    string(4) "Blue"
    ["size"]=>
    string(1) "X"
    ["weight"]=>
    int(150)
  }
  [1]=>
  array(3) {
    ["color"]=>
    string(3) "Red"
    ["size"]=>
    string(1) "X"
    ["weight"]=>
    int(150)
  }
  [2]=>
  array(3) {
    ["color"]=>
    string(4) "Pink"
    ["size"]=>
    string(1) "X"
    ["weight"]=>
    int(150)
  }
  [3]=>
  array(3) {
    ["color"]=>
    string(4) "Blue"
    ["size"]=>
    string(1) "S"
    ["weight"]=>
    int(150)
  }
  [4]=>
  array(3) {
    ["color"]=>
    string(3) "Red"
    ["size"]=>
    string(1) "S"
    ["weight"]=>
    int(150)
  }
  [5]=>
  array(3) {
    ["color"]=>
    string(4) "Pink"
    ["size"]=>
    string(1) "S"
    ["weight"]=>
    int(150)
  }
  [6]=>
  array(3) {
    ["color"]=>
    string(4) "Blue"
    ["size"]=>
    string(1) "X"
    ["weight"]=>
    int(250)
  }
  [7]=>
  array(3) {
    ["color"]=>
    string(3) "Red"
    ["size"]=>
    string(1) "X"
    ["weight"]=>
    int(250)
  }
  [8]=>
  array(3) {
    ["color"]=>
    string(4) "Pink"
    ["size"]=>
    string(1) "X"
    ["weight"]=>
    int(250)
  }
  [9]=>
  array(3) {
    ["color"]=>
    string(4) "Blue"
    ["size"]=>
    string(1) "S"
    ["weight"]=>
    int(250)
  }
  [10]=>
  array(3) {
    ["color"]=>
    string(3) "Red"
    ["size"]=>
    string(1) "S"
    ["weight"]=>
    int(250)
  }
  [11]=>
  array(3) {
    ["color"]=>
    string(4) "Pink"
    ["size"]=>
    string(1) "S"
    ["weight"]=>
    int(250)
  }
}

A function that makes what you want

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

$variants = [
    'color' => ['Blue', 'Red', 'Pink'],
    'size' => ['X', 'S']
];

$result = get_combinations($variants);

The result will be like you want it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM