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
}