Last active
July 24, 2019 10:36
-
-
Save jlabs/e87c4aee8dd72dac4c1764279394136e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Array given from 'api' | |
$arr = array( | |
"action: Added; quantity: 1; item_code: RNA1; product_name: Mens Organic T-Shirt; colour: White; size: XL;", | |
"action: Subtracted; quantity: 7; item_code: RNC1; product_name: Kids Basic T-shirt; colour: Denim Blue; size: 3-4y", | |
"action: Added; quantity: 20; item_code: RNV1; product_name: Gift Voucher; style: Mens; value: £20" | |
); | |
// New array to hold the final output | |
$newArr = array(); | |
// Loop through each array item/line | |
foreach ($arr as $line) { | |
// Split the current item/line by the ; character into a new array, which splits the string into pairs | |
$tempArr = explode ('; ', $line); | |
// Loop through the new array created from the split | |
foreach ($tempArr as $pair) { | |
// Create a list using a key/value pair | |
// The $k comes from item [0] of the array created from the split of the : charater | |
// The $v comes from item [1] of the array | |
list ($k,$v) = explode (': ',$pair); | |
// Temp array that assigns the key ($k) to the value of $v | |
$pairs[$k] = $v; | |
} | |
// Push the temp array into the output array | |
array_push($newArr, $pairs); | |
} | |
// Print the output array | |
print_r($newArr); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment