<?php
$arr = [
"action: Added; amount: 1; code: RNA1; name: Mens Organic T-shirt; colour: White; size: XL",
"action: Subtracted; amount: 7; code: RNC1; name: Kids Basic T-shirt; colour: Denim Blue; size: 3-4y",
"action: Added; amount: 20; code: RNV1; name: Gift Voucher; style: Mens; value: £20",
];
$keyed = array_reduce($arr, function($collector, $value) {
$collector[] = array_reduce(explode('; ', trim($value, '; ')), function($collector, $value) {
$parts = explode(': ', $value);
$collector[$parts[0]] = $parts[1];
return $collector;
}, []);
return $collector;
}, []);
var_dump($keyed);
- Output for 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
- array(3) {
[0]=>
array(6) {
["action"]=>
string(5) "Added"
["amount"]=>
string(1) "1"
["code"]=>
string(4) "RNA1"
["name"]=>
string(20) "Mens Organic T-shirt"
["colour"]=>
string(5) "White"
["size"]=>
string(2) "XL"
}
[1]=>
array(6) {
["action"]=>
string(10) "Subtracted"
["amount"]=>
string(1) "7"
["code"]=>
string(4) "RNC1"
["name"]=>
string(18) "Kids Basic T-shirt"
["colour"]=>
string(10) "Denim Blue"
["size"]=>
string(4) "3-4y"
}
[2]=>
array(6) {
["action"]=>
string(5) "Added"
["amount"]=>
string(2) "20"
["code"]=>
string(4) "RNV1"
["name"]=>
string(12) "Gift Voucher"
["style"]=>
string(4) "Mens"
["value"]=>
string(4) "£20"
}
}
- Output for 8.3.5
- Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0
array(3) {
[0]=>
array(6) {
["action"]=>
string(5) "Added"
["amount"]=>
string(1) "1"
["code"]=>
string(4) "RNA1"
["name"]=>
string(20) "Mens Organic T-shirt"
["colour"]=>
string(5) "White"
["size"]=>
string(2) "XL"
}
[1]=>
array(6) {
["action"]=>
string(10) "Subtracted"
["amount"]=>
string(1) "7"
["code"]=>
string(4) "RNC1"
["name"]=>
string(18) "Kids Basic T-shirt"
["colour"]=>
string(10) "Denim Blue"
["size"]=>
string(4) "3-4y"
}
[2]=>
array(6) {
["action"]=>
string(5) "Added"
["amount"]=>
string(2) "20"
["code"]=>
string(4) "RNV1"
["name"]=>
string(12) "Gift Voucher"
["style"]=>
string(4) "Mens"
["value"]=>
string(4) "£20"
}
}
preferences:
174.02 ms | 403 KiB | 181 Q