Search code examples
phparrayskey-valueexplodeimplode

Associative Array issue with PHP


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.


Solution

  • <?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",
    );
    
    //create new array to hold new values
    $new_arr = [];
    
    //loop through strings
    foreach($arr as $v) {
    
        //break strings into "chunks" (e.g "action: Added", "quantity: 1", etc)
        $chunks = explode('; ', $v);
    
        //create temporary array inside "string loop"
        $tmp = [];
    
        //loop through each chunk
        foreach($chunks as $chunk) {
    
            //split chunks into key => values (e.g "action", "added")
            $key_value = explode(': ', $chunk);
    
            //add split chunk to `$tmp` array, using `[0]` as key and `[1]` as value
            $tmp[$key_value[0]] = $key_value[1];
        }
    
        //after "chunk loop", add `$tmp` to `$new_arr`
        $new_arr[] = $tmp;
    }
    
    print_r($new_arr);
    

    Output:

    Array
    (
        [0] => Array
            (
                [action] => Added
                [quantity] => 1
                [item_code] => RNA1
                [product_name] => Mens Organic T-shirt
                [colour] => White
                [size] => XL
            )
    
        [1] => Array
            (
                [action] => Subtracted
                [quantity] => 7
                [item_code] => RNC1
                [product_name] => Kids Basic T-shirt
                [colour] => Denim Blue
                [size] => 3-4y
            )
    
        [2] => Array
            (
                [action] => Added
                [quantity] => 20
                [item_code] => RNV1
                [product_name] => Gift Voucher
                [style] => Mens
                [value] => £20
            )
    
    )