1

Here is my array:

[options] => Array
    (
        [1] => Array
            (
                [0] => Red
                [1] => Yellow
                [2] => Blue
                [3] => Green
            )

        [2] => Array
            (
                [0] => 28
                [1] => 30
                [2] => 32
                [3] => 34
            )

        [3] => Array
            (
                [0] => Short Neck
                [1] => Full Neck
                [2] => Round Neck
            )

        [5] => Array
            (
                [0] => Bodycon
                [1] => Empire Waist
                [2] => High-Low
            )

    )

Want to create string like this:
Red-28-Short Neck-Bodycon
Red-28-Short Neck-Empire Waist
Red-28-Short Neck-High-Low

Red-28-Full Neck-Bodycon
Red-28-Full Neck-Empire Waist
Red-28-Full Neck-High-Low

Red-28-Round Neck-Bodycon
Red-28-Round Neck-Empire Waist
Red-28-Round Neck-High-Low

Red-30-Short Neck-Bodycon
Red-30-Short Neck-Empire Waist
Red-30-Short Neck-High-Low

Red-30-Full Neck-Bodycon
Red-30-Full Neck-Empire Waist
Red-30-Full Neck-High-Low

Red-30-Round Neck-Bodycon
Red-30-Round Neck-Empire Waist
Red-30-Round Neck-High-Low

and so on...

For each value

Thanks

Martin Zeitler
  • 1
  • 19
  • 155
  • 216
vipinbairagi
  • 123
  • 7
  • you can use recursive or you can create a loop inside a loop and. . .. depends upon your requirement – nitishk72 Sep 15 '18 at 16:30
  • 1
    There seems to be good answers in this thread: https://stackoverflow.com/questions/6311779/finding-cartesian-product-with-php-associative-arrays Then just glue the resulting arrays with proper separators. – Andrius Rimkus Sep 15 '18 at 16:37
  • 1
    Possible duplicate of [Finding cartesian product with PHP associative arrays](https://stackoverflow.com/questions/6311779/finding-cartesian-product-with-php-associative-arrays) – Martin Zeitler Sep 15 '18 at 16:38

3 Answers3

1

You can use a recursive function to build the result array, for example:

function combine($arr) {
    if (count($arr) === 1) {
        return array_shift($arr);
    } 
    $items = array_shift($arr);

    $data = [];
    foreach ($items as $item) {
        foreach (combine($arr) as $value) {
            $data[] = $item . ' ' . $value;
        }
    } 
    return $data;
}

and using example:

$items = [
    [            
        'Red',
        'Yellow',
        'Blue',
        'Green',
    ],
    [
        28,
        30,
        32,
        34
    ],
    [
        'Short Neck',
        'Full Neck',
        'Round Neck'
    ],
    [
        'Bodycon',
        'Empire Waist',
        'High-Low'
    ]
];

$result = combine($items);
Maksym Fedorov
  • 6,383
  • 2
  • 11
  • 31
0

You can use the cartesian() function from the thread in the comments, or a simple routine like so:

$join = [];
foreach( $myarray[ 'options' ] as $block ) :

    foreach( $block as $i => $value ) :

        $join[ $i ][] = $value;

    endforeach;

endforeach;

foreach( $join as $string ) :

    echo implode( '-', $string ) . '<br>';

endforeach;

Output:

Red-28-Short Neck-Bodycon
Yellow-30-Full Neck-Empire Waist
Blue-32-Round Neck-High-Low
Green-34

Not very pretty, but should get the job done for a single use case.

Hans
  • 1,156
  • 9
  • 13
0

Given your array:

$items = [
    1=>[            
        'Red',
        'Yellow',
        'Blue',
        'Green',
    ],
    2=>[
        28,
        30,
        32,
        34
    ],
    3=>[
        'Short Neck',
        'Full Neck',
        'Round Neck'
    ],
    5=>[
        'Bodycon',
        'Empire Waist',
        'High-Low'
    ]
];

This is the simpliest and less consuming way to achieve it

$items=array_values($items);
$result=[];


foreach($items[0] as $k1=>$v1){
    foreach($items[1] as $k2=>$v2){
        foreach($items[2] as $k3=>$v3){
            foreach($items[3] as $k4=>$v4){
                $result[]=$v1.' '.$v2.' '.$v3.' '.$v4;
            }
        }
    }   
}

Given your array if you want to remove a step from the code above you can use instead

$result=[];


    foreach($items[1] as $k1=>$v1){
        foreach($items[2] as $k2=>$v2){
            foreach($items[3] as $k3=>$v3){
                foreach($items[5] as $k4=>$v4){
                    $result[]=$v1.' '.$v2.' '.$v3.' '.$v4;
                }
            }
        }   
    }

Note that we remove the step of array_values

These codes output an array of 144 entries representing all the combinations in the expected order.

Elementary
  • 1,443
  • 1
  • 7
  • 17