1

I have a weird behaviour in PHP when merging an array into another.
The index of numerical Array elements just change from like 26 to 1 and 27 to 2 and that keeps going on for all numerical Indexes.

How do I prevent that from happening and keep all Index like they are? My Code looks like the following:

foreach ($options as $key => $option) {
    $optionName = $option->{"attributeName"};
    $subOptions = array_flip($option->{"predefinedStringValues"});
    if (!isset($optionList[$optionName])) {
        $optionList[$optionName] = $subOptions;
    } else {
        $optionList[$optionName] = array_merge($optionList[$optionName], $subOptions);
    }
}

$options is a List of Options i have, which has options like "Size" or "Colour" and can appear multiple times, with different Id.
I want to merge them together so my $optionList only has 1 Index that is called "Size" and has all sizes in that.
So in the end it kinda looks like this:

$optionList = [
    "Sizes" = [
        "small" = 0,
        "extrasmall = T0" = 1,
        "Onesize" = 2,
        "medium" = 3,
        "extra large" = 4,
        "28/34" = 5,
        ...
    ],
    "Colours" = [
        "red" = 1,
        "blue" = 2,
        "green" = 27,
        ...
    ],
    ...
]

Array1 is my first appearence of "Sizes" and Array2 is my second.
Array3 is what my Code builds from that and Array4 is what i expect it to look like.

I have these 2 Arrays, which are already flipped:

Array1 = {
    [small] => 0
    [extrasmall = T0] => 1
    [Onesize] => 2
    [medium] => 3
    [extra large] => 4
    [28/34] => 5
    [28/32] => 6
    [26/32] => 7
    [T] => 8
    [31/32] => 9
    [31/34] => 10
    [large] => 11
    [medium = T2] => 12
    [small = T1] => 13
    [l] => 14
    [m] => 15
    [large =T3] => 16
    [1] => 17
    [27/34] => 18
    [29/34] => 19
    [27/32] => 20
    [s] => 21
    [30/32] => 22
    [30/34] => 23
    [32/34] => 24
    [xs] => 25
    [29/32] => 26
}

Array2 = {
    [Onesize] => 0
    [24] => 1
    [25] => 2
    [36] => 3
    [26] => 4
    [37] => 5
    [27] => 6
    [38] => 7
    [28] => 8
    [39] => 9
    [29] => 10
    [M/L] => 11
    [L] => 12
    [M] => 13
    [S/M] => 14
    [S] => 15
    [xl] => 16
    [XS] => 17
    [40] => 18
    [30] => 19
    [41] => 20
    [31] => 21
}

And they get merged to this:

Array3 = {
    [small] => 0
    [extrasmall = T0] => 1
    [Onesize] => 0
    [medium] => 3
    [extra large] => 4
    [28/34] => 5
    [28/32] => 6
    [26/32] => 7
    [T] => 8
    [31/32] => 9
    [31/34] => 10
    [large] => 11
    [medium = T2] => 12
    [small = T1] => 13
    [l] => 14
    [m] => 15
    [large =T3] => 16
    [0] => 17
    [27/34] => 18
    [29/34] => 19
    [27/32] => 20
    [s] => 21
    [30/32] => 22
    [30/34] => 23
    [32/34] => 24
    [xs] => 25
    [29/32] => 26
    [1] => 1
    [2] => 2
    [3] => 3
    [4] => 4
    [5] => 5
    [6] => 6
    [7] => 7
    [8] => 8
    [9] => 9
    [10] => 10
    [M/L] => 11
    [L] => 12
    [M] => 13
    [S/M] => 14
    [S] => 15
    [xl] => 16
    [XS] => 17
    [11] => 18
    [12] => 19
    [13] => 20
    [14] => 21
}

The expected result should be like this:

Array4 = {
    [small] => 0
    [extrasmall = T0] => 1
    [Onesize] => 2
    [medium] => 3
    [extra large] => 4
    [28/34] => 5
    [28/32] => 6
    [26/32] => 7
    [T] => 8
    [31/32] => 9
    [31/34] => 10
    [large] => 11
    [medium = T2] => 12
    [small = T1] => 13
    [l] => 14
    [m] => 15
    [large =T3] => 16
    [1] => 17
    [27/34] => 18
    [29/34] => 19
    [27/32] => 20
    [s] => 21
    [30/32] => 22
    [30/34] => 23
    [32/34] => 24
    [xs] => 25
    [29/32] => 26
    [24] => 1
    [25] => 2
    [36] => 3
    [26] => 4
    [37] => 5
    [27] => 6
    [38] => 7
    [28] => 8
    [39] => 9
    [29] => 10
    [M/L] => 11
    [L] => 12
    [M] => 13
    [S/M] => 14
    [S] => 15
    [xl] => 16
    [XS] => 17
    [40] => 18
    [30] => 19
    [41] => 20
    [31] => 21
}
  • `array_merge` does re-order numeric keys. I would go the other way - collecting array of values instead of keys with "useless" values. – cottton Nov 15 '21 at 13:16
  • I access the array using the name of the option and not the id. I can't change that without huge performance loss. So I need the array like that, but can't work with it when it changes my numeric keys. – Grimclaw Draven Nov 15 '21 at 13:25
  • Can you provide a small `$options` and `$optionList` example? Atm i do no really see what you start from and what you expect at the end. – cottton Nov 15 '21 at 13:31
  • I edited the post with some additional information. Please tell me if thats enought – Grimclaw Draven Nov 15 '21 at 13:50
  • 1
    Does this answer your question? [PHP: merge two arrays while keeping keys instead of reindexing?](https://stackoverflow.com/questions/3292044/php-merge-two-arrays-while-keeping-keys-instead-of-reindexing) – TheGentleman Nov 15 '21 at 13:51
  • Yes adding up the arrays did the needed job, ty a lot – Grimclaw Draven Nov 15 '21 at 14:28

0 Answers0