-1

this question is a little bit similar to my old one here.

But now I've got only one dynamically filled array

Array ( 
    [8] => 3-Blue
    [9] => 3-Red
    [10] => 4-L
    [11] => 4-XL
    ...
    ...
)

for each number in front of the value, I need a new array. So for this example I need these two

Array ( 
    [8] => Blue
    [9] => Red
)

Array (
    [10] => L
    [11] => XL
)

The key should stay the same. If this is not possible, than the keys should be in the value L - 10

than I need to combine those arrays, and get the possible combinations without duplicates in a new array like this.

Array (
    [0] => Blue | L - 8,10
    [1] => Blue | XL - 8,11
    [2] => Red | L - 9,10
    [3] => Red | XL - 9,11
)

after each value, there must be the keys form the arrays before.

is this even possible?! and if yes, how?

Community
  • 1
  • 1
invictus
  • 825
  • 1
  • 9
  • 33
  • 2
    Yes it's possible. What have you tried? – Kodlee Yin Jun 30 '14 at 21:34
  • I tried to play around with a few kinds of foreach loops but I can't figure out how to achieve this. @user574632 Booth would be great:D but I think with a clear code, it would be better for me to understand how it works. – invictus Jun 30 '14 at 21:40
  • Where do all the these strings come from? How did they end up like this? – Sverri M. Olsen Jun 30 '14 at 21:44
  • they key of the first array, is the id of the option object, the number in the value string is the id of the variation object which contains multiple options. the second part of the value is the title of the option. I didn't know another approach how to solve my problem. – invictus Jun 30 '14 at 21:47
  • possible duplicate of [Finding cartesian product with PHP associative arrays](http://stackoverflow.com/questions/6311779/finding-cartesian-product-with-php-associative-arrays) – Mr. Llama Jun 30 '14 at 21:49
  • 1
    See this question on finding the cartesian product of associative arrays: https://stackoverflow.com/questions/6311779/finding-cartesian-product-with-php-associative-arrays – Mr. Llama Jun 30 '14 at 21:49
  • @cSGermany That is not what I asked. Where does the data come from? How were the strings, in the arrays, created? You should not be parsing strings like this, in order to get at the values. The values should be stored separately in a database somewhere, so that you can access them directly. – Sverri M. Olsen Jun 30 '14 at 21:51
  • @Mr.Llama thank you, that seems helpfull – invictus Jun 30 '14 at 21:52
  • @SverriM.Olsen Sry than I misunderstood your question. The strings are created from this code http://pastie.org/9341634. They are stored in a Database. But now I want to create a new Dataobject for each variation. Don't know how to solve this another way – invictus Jun 30 '14 at 21:54
  • @cSGermany Okay. You have the values stored in variables. Why are you making them into strings, and then extracting them again? It makes absolutely no sense. – Sverri M. Olsen Jun 30 '14 at 21:56
  • @SverriM.Olsen I updated my comment before. This should answer your question – invictus Jun 30 '14 at 21:57

1 Answers1

0

You can simply iterate both arrays using nested foreach ($arr as $key => $val) loop.
Here I use the sprintf function instead of string concatenation, simply because it makes the intended output format more clear.

$color_array = array(8 => "Blue", 9 => "Red");
$size_array  = array(10 => "L", 11 => "XL");

$result = array();

// For each key => value pair in the color array
foreach ($color_array as $color_key => $color) {

    // For each key => value pair in the size array
    foreach ($size_array as $size_key => $size) {

        // Generate the result string with "sprintf", then append it to the result array
        $result[] = sprintf("%s | %s - %d,%d", $color, $size, $color_key, $size_key);
    }
}

var_dump($result);
// array(4) {
//   [0]=> string(15) "Blue | L - 8,10"
//   [1]=> string(16) "Blue | XL - 8,11"
//   [2]=> string(14) "Red | L - 9,10"
//   [3]=> string(15) "Red | XL - 9,11"
// }

You can dedupe the result array using array_unique. However, if both of your input arrays are unique, your output set should be unique as well.

Mr. Llama
  • 20,202
  • 2
  • 62
  • 115