Here, I have an array.
$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",
I have taken some steps to try and convert this array into a key-pair array by doing the following:
// Split the array into strings.
$imploded = implode(" ",$arr);
//echo $imploded;
// Seperate the strings by the ";" character.
$exploded = explode("; ",$imploded);
//print_r($exploded);
// The elements are now in an array.
// Here I need to separate them into key value pairs.
I used the $explode function to split the $imploded string by the "; " character to make the data easier to work with. This is the result of printing the $exploded array:
Array ( [0] => action: Added [1] => quantity: 1 [2] => item_code: RNA1 [3] => product_name: Mens Organic T-shirt [4] => colour: White [5] => size: XL action: Subtracted [6] => quantity: 7 [7] => item_code: RNC1 [8] => product_name: Kids Basic T-shirt [9] => colour: Denim Blue [10] => size: 3-4y action: Added [11] => quantity: 20 [12] => item_code: RNV1 [13] => product_name: Gift Voucher [14] => style: Mens [15] => value: £20 )
I have tried experimenting with the array_combine function, however I don't think it is working because the keys and pairs are in the same elements.
$result = array(
array_combine($exploded[0], $exploded[1]));
//print_r($result);
I could potentially try splitting the elements with the ": " character, but i'm not sure how i'd go about doing this. I'm also quite new to PHP, so i'd appreciate some simple code and explanations :)
EDIT: I'd like the resulting array to look something like this...
'action' => '',
'quantity' => '',
'item_code' => '',
'product_name' => '',
'colour' => '',
'size' => '',