-1

I'm currently working on an open source ecommerce system for a CMS I use. I am currently working on product variations. I'm having trouble with calculating each possible option for the variations.

The array currently is like this:

Array
(
    [Size] => Array
        (
            [0] => Small
            [1] => Medium
            [2] => Large
        )

    [Colour] => Array
        (
            [0] => Blue
            [1] => Red
            [2] => Green
        )

)

I want it to return something like this:

Array
(
    [0] => Size=Small, Colour=Blue
    [1] => Size=Small, Colour=Red
    [2] => Size=Small, Colour=Green
    [3] => Size=Medium, Colour=Blue
    [4] => Size=Medium, Colour=Red
    [5] => Size=Medium, Colour=Green
    [6] => Size=Large, Colour=Blue
    [6] => Size=Large, Colour=Red
    [8] => Size=Large, Colour=Green
)

I have tried a few approaches but it's starting to rack my brains. This should also be scaleable for example Size=Large, Colour=Red, Material=Fabric.

Any help will be greatly appreciated.

Tom
  • 125
  • 9
  • And `Material=Fabric` came from where?? – Yash Parekh Nov 10 '17 at 12:57
  • If the original array was ` [Size] => Array ( [0] => Small [1] => Medium [2] => Large ) [Colour] => Array ( [0] => Blue [1] => Red [2] => Green ) [Material] => Array ( [0] => Fabric [1] => Leather ) ) ` – Tom Nov 10 '17 at 12:59
  • I've answered a very similar question some time ago. See [here](https://stackoverflow.com/questions/38866706/recursive-function-to-iterate-through-t-shirt-variants/38867105#38867105). – Andrei Nov 10 '17 at 13:00
  • @Tom, if you want `Material` to be also considered - update your expected output – RomanPerekhrest Nov 10 '17 at 13:01

1 Answers1

0
  <?php

$array=array
(
    'size' => Array
    (
        'Small',
        'Medium',
        'Large'
        ),

    'Color' => Array
(
  'Blue','Red','Green'
        )

);
$arrayFirstKey=(array_keys($array)[0]);
$arraySecondKey=(array_keys($array)[1]);

$i=0;
$newArray=array();
foreach($array[$arrayFirstKey] as $size){
     foreach($array[$arraySecondKey]  as $color) {
    $newArray[] = $arrayFirstKey.'=' . $size .','. $arraySecondKey.'=' . $color;
    $i++;
    }
}
echo "<pre>";
print_r($newArray);

And the output is:

    Array
(
    [0] => size=Small, Colour=Blue
    [1] => size=Small, Colour=Red
    [2] => size=Small, Colour=Green
    [3] => size=Medium, Colour=Blue
    [4] => size=Medium, Colour=Red
    [5] => size=Medium, Colour=Green
    [6] => size=Large, Colour=Blue
    [7] => size=Large, Colour=Red
    [8] => size=Large, Colour=Green
)

Edited:

It crates the key-values dynamic. I provide the output of the example you set in the comments. The only thing that changed was the array to add the new values nothing else from the functionality of the code.

Array
(
    [0] => year=1992,Type=Champaign
    [1] => year=1992,Type=Wine
    [2] => year=1993,Type=Champaign
    [3] => year=1993,Type=Wine
)
pr1nc3
  • 8,108
  • 3
  • 23
  • 36
  • Thanks, however they key isn't known. They key could be anything as it's user defined, for example it could be `Array( ['year'] => array( '1992', '1993' ), ['Type'] => array( 'Champaign', 'Wine' ) )` – Tom Nov 10 '17 at 13:15
  • Modified my answer. Now it works for any key-value. Give it a try – pr1nc3 Nov 10 '17 at 13:22
  • Thanks, closer. However the user could define 1 set or options or 5, or 100. Sorry to be a pain. – Tom Nov 10 '17 at 13:24
  • Hey pr1nc3, I just did two keys because writing 5 would take ages. The main thing is, there could be 1 or 100. It's truly up to the user. – Tom Nov 10 '17 at 13:27