-4
Array
(
    [Colour] => Array
        (
            [Red] => Red
            [Blue] => Blue
        )

    [Size] => Array
        (
            [Small] => Small
            [Medium] => Medium
            [Large] => Large
        )

    [Brand] => Array
        (
            [Nike] => Nike
            [Adidas] => Adidas
        )

)

how do i get multi dimension array to get this variation output:

Red - Small - Nike
Red - Medium - Nike
Red - Large - Nike
Red - Small - Adidas
Red - Medium - Adidas
Red - Large - Adidas
Blue - Small - Nike
Blue - Medium - Nike
Blue - Large - Nike
Blue - Small - Adidas
Blue - Medium - Adidas
Blue - Large - Adidas
localheinz
  • 9,179
  • 2
  • 33
  • 44
tonoslfx
  • 3,422
  • 15
  • 65
  • 107

1 Answers1

2

What you are looking for is a cartesian product.

Combine the values like this:

$data = [
    'Colour' => [
        'Red' => 'Red',
        'Blue' => 'Blue',
    ],
    'Size' => [
        'Small' => 'Small',
        'Medium' => 'Medium',
        'Large' => 'Large',
    ],
    'Brand' => [
        'Nike' => 'Nike',
        'Adidas' => 'Adidas',
    ],
];

$combined = [];

foreach ($data['Colour'] as $colour) {
    foreach ($data['Brand'] as $brand) {
        foreach ($data['Size'] as $size) {
            $combined[] = implode(' - ', [
                $colour,
                $size,
                $brand
            ]);
        }
    }
}

var_dump($combined);

For reference, see:

For an example, see:

localheinz
  • 9,179
  • 2
  • 33
  • 44