-2

Can you help me with generating caresian product. It is similar to this stackoverflow. I want generate inputs so I need keep ID.

Example:

my input data:

[
  1 => [
    id => 1,
    name => "Color",
    options => [
       5 => [
         id => 5,
         name => "Red"
       ],
       6 => [
         id => 6,
         name => "Blue"
       ]
    ]
  ],
 2 => [
    id => 2,
    name => "Size",
    options => [
       7 => [
         id => 7,
         name => "S"
       ],
       8 => [
         id => 8,
         name => "M"
       ]
    ]
  ],

  // etc
]

result I expect:

[
 "5-7" => "Red / S",
 "5-8" => "Red / M",
 "6-7" => "Blue / S",
 "6-8" => "Blue / M"
]

I need generic function for any number of properties/options..

Community
  • 1
  • 1
user3359653
  • 41
  • 1
  • 1
  • 4
  • what have you tried so far? please post your code that you have tried first then we can see where you are going wrong. – Clay Dec 28 '15 at 08:30
  • I tried solution from other stackoverflow I wrote above, but it is not for my purpose. – user3359653 Dec 28 '15 at 08:34

2 Answers2

0

Nested loop man, each entry of array 1 has to be linked with every entry of array2.

$finalArray = array();

foreach (array1 as $key1 as $value1){
  foreach (array2 as $key2 as$value2){
   echo  $value1 . " - " .$value2."<br/>";
   $finalArray[$key1.'-'.$key2] =   $value1 ." - ".$value2;
 }
}

the finalArray will be having what you need.

Danyal Sandeelo
  • 12,196
  • 10
  • 47
  • 78
  • I need dynamics "function" for any number of "properties". There could be 2, 3, 4 .. any number of properties with options :) – user3359653 Dec 28 '15 at 08:32
  • that will work..just declare a new array and keep putting the key and values..let me update my answer – Danyal Sandeelo Dec 28 '15 at 08:33
  • I see, but I can not edit function every time I add new properties, It must be generic for any number of properties. Maybe we don't understand each other :) – user3359653 Dec 28 '15 at 08:39
  • The post that you mentioned, did you try that solution? I got what you mean, you need dynamic number of arrays.. – Danyal Sandeelo Dec 28 '15 at 08:40
  • I tried that. But the output is different from my need. :) It combine all field so also id, name atc.. It similar problem but not same, so their solution cant fit to my.. – user3359653 Dec 28 '15 at 08:44
  • @DanyalSandeelo I think he means that n number of sets/properties? Meaning its not just Color and Size just like in he's example. it may be 3 sets/properties – Ceeee Dec 28 '15 at 08:47
0

That is actually working code for now but don't know how much is efficient.

// filter out properties without options
$withOptions = array_filter($properties, function($property) {
    return count($property['options']) > 0;
});

$result = [];

$skipFirst = true;

foreach ($withOptions as $property) {

    if ($skipFirst) {

        foreach (reset($withOptions)['options'] as $id => $option) {
            $result[$id] = $option['name'];
        }

        $skipFirst = false;
        continue;
    }

    foreach ($result as $code => $variant) {    
        foreach ($property['options'] as $id => $option) {
            $new = $code . "-" . $id;
            $result[$new] = $variant . " / " . $option['name'];
            unset($result[$code]);
        }
    }
}
user3359653
  • 41
  • 1
  • 1
  • 4