簡體   English   中英

在 PHP 中將兩個不同長度的 arrays 相乘?

[英]Multiply two arrays with different length in PHP?

我有一個動態數組:

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

我期望這個:

$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']
];

結果是所有數組長度的乘積。 我已經搜索但尚未找到解決方案。 希望任何人都可以提供幫助。 非常感謝!

這有點棘手,您需要為此做好准備和聚合步驟。 但不回退到遞歸是可能的:

<?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);

其 output 為:

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

function 可以滿足您的需求

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

結果會如你所願。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM