Associative Array issue

Hi everyone. I am building an algorithm around a theoretical scenario of an array being stored in an API, meaning that I am not allowed to edit it, and I am trying to grab data from said array and turn it into an associative array using the implode and explode functions. I have made good progress, and this is my code so far…

<?php

$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",
);

// Split the array into strings.
$imploded1 = implode("; ", $arr);

//echo $imploded;

// Seperate the strings by the ";" character.
$exploded1 = explode("; ",$imploded1);
// This gives me the keys and pairs in the same index.

$imploded2 = implode("\n", $exploded1);
// print_r($formatimploded);

$result = array_column(array_map(function($v) {
	return explode(":", $v);
}, explode(" ", $imploded2)), 1, 0);
print_r($result);

The program almost works. The issue i’m having is that the values of the keys are not being added to the array, and i’m not sure why this is. This is the output:

Array
(
[action] =>
[quantity] =>
[item_code] =>
[product_name] =>
[colour] =>
[size] =>
[style] =>
[value] =>
)

Can someone explain this to me? Any help is much appreciated.

Do not make more threads on the same code. You already have a thread going here
https://www.phphelp.com/t/converting-array-elements-to-a-key-value-pair/32483

Sponsor our Newsletter | Privacy Policy | Terms of Service