-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.

4
  • And Material=Fabric came from where?? Commented Nov 10, 2017 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 ) ) ` Commented Nov 10, 2017 at 12:59
  • I've answered a very similar question some time ago. See here. Commented Nov 10, 2017 at 13:00
  • @Tom, if you want Material to be also considered - update your expected output Commented Nov 10, 2017 at 13:01

1 Answer 1

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
)
Sign up to request clarification or add additional context in comments.

4 Comments

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' ) )
Modified my answer. Now it works for any key-value. Give it a try
Thanks, closer. However the user could define 1 set or options or 5, or 100. Sorry to be a pain.
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.