<?php
$arr = array(
"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");
foreach ($arr as $string) {
//Build array
preg_match_all("/ [ ]?([^:]+): ([^;]+)[ ;]? /x", $string, $p);
$array = array_combine($p[1], $p[2]);
//Print it or do something else with it
print_r($array);
}
- 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.31, 8.2.0 - 8.2.26, 8.3.0 - 8.3.14, 8.4.1
- Array
(
[action] => Added
[amount] => 1
[code] => RNA1
[name] => Mens Organic T-shirt
[colour] => White
[size] => XL
)
Array
(
[action] => Subtracted
[amount] => 7
[code] => RNC1
[name] => Kids Basic T-shirt
[colour] => Denim Blue
[size] => 3-4y
)
Array
(
[action] => Added
[amount] => 20
[code] => RNV1
[name] => Gift Voucher
[style] => Mens
[value] => £20
)
preferences:
83.81 ms | 408 KiB | 5 Q