-2

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.

Loencodes
  • 29
  • 6

2 Answers2

2
<?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
        )

)
GrumpyCrouton
  • 8,486
  • 7
  • 32
  • 71
1

If you're in charge of how this data is actually stored in the first place, you'd be better off storing it in a more standardised format, such as JSON. Your data will be far easier to work with.

Here's how you would store one of your items as JSON. Note that it's only a minor change to your current usage of semi-colons and non-quoted values.

$json = '{"action": "Added", "quantity": "1", "item_code": "RNA1", "product_name": "Mens Organic T-shirt", "colour": "White", "size": "XL"}';

In PHP you can then simply use json_decode() to convert it to an object:

$item = json_decode($json);

You can access the properties easily like so:

$item->quantity
$item->colour
$item->size

If you really want an array instead, for some reason, you can just do this instead:

$item = json_decode($json, true);

Which gives:

Array
(
    [action] => Added
    [quantity] => 1
    [item_code] => RNA1
    [product_name] => Mens Organic T-shirt
    [colour] => White
    [size] => XL
)
BadHorsie
  • 14,135
  • 30
  • 117
  • 191