I have a string with the following format:
$data = "sku=32300905,color=Dark Brown|sku=32300899,color=Blue|sku=32300900,color=Grey|sku=32300914,color=light Brown";
And I want to transform this string into an array of associative arrays as shown below:
Array
(
[0] => Array
(
[sku] => 32300905
[color] => Dark Brown
)
[1] => Array
(
[sku] => 32300899
[color] => Blue
)
[2] => Array
(
[sku] => 32300900
[color] =>
)
[3] => Array
(
[sku] => 32300914
[color] => light Brown
)
[4] => Array
(
[sku] => 12345678
[color] =>
)
)
What would be the most efficient way to achieve this conversion in PHP?
2
Answers
You can try this way to get you response
Or you can also do like this
Or you could use a simple regex:
(Assumes your SKU codes are all numeric, and that your color values don’t contain the ‘|’ character but could contain a comma)