<?php
$product = [
'title' => 'Modest Swimsuit - Full body',
'description' => 'UV +50 Protection - Chlorine Resistant - Water Resistant - Quick Drying - Maximum Breathe Ability- Sea, Water Resistant',
'price' => 59.95,
'stock' => 20,
];
$attributes = [
['name' => 'Brand', 'value' => 'Modestly Active Swimwear'],
['name' => 'Colour', 'value' => 'Black and Light Blue'],
['name' => 'size', 'value' => 'small'],
['name' => 'size', 'value' => 'medium'],
['name' => 'size', 'value' => 'large'],
];
$pivoted = [];
foreach ($attributes as ['name' => $name, 'value' => $value]) {
$name = strtolower($name);
if (isset($pivoted[$name])) {
$pivoted[$name] = (array) $pivoted[$name];
$pivoted[$name][] = $value;
} else {
$pivoted[$name] = $value;
}
}
var_export($product + $pivoted);
- Output for 8.1.0 - 8.1.28, 8.2.0 - 8.2.17, 8.2.19, 8.3.0 - 8.3.7
- array (
'title' => 'Modest Swimsuit - Full body',
'description' => 'UV +50 Protection - Chlorine Resistant - Water Resistant - Quick Drying - Maximum Breathe Ability- Sea, Water Resistant',
'price' => 59.95,
'stock' => 20,
'brand' => 'Modestly Active Swimwear',
'colour' => 'Black and Light Blue',
'size' =>
array (
0 => 'small',
1 => 'medium',
2 => 'large',
),
)
- Output for 8.2.18
- Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.2.18/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.2.18/modules/sodium.so.so (/usr/lib/php/8.2.18/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0
array (
'title' => 'Modest Swimsuit - Full body',
'description' => 'UV +50 Protection - Chlorine Resistant - Water Resistant - Quick Drying - Maximum Breathe Ability- Sea, Water Resistant',
'price' => 59.95,
'stock' => 20,
'brand' => 'Modestly Active Swimwear',
'colour' => 'Black and Light Blue',
'size' =>
array (
0 => 'small',
1 => 'medium',
2 => 'large',
),
)
preferences:
47.97 ms | 403 KiB | 62 Q