0

I have built a program that takes data from an array full of strings, and turns the data into an appropriately formatted array. This is the original array:

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

And this is the new array with the data appropriately formatted.

Array
(
    [0] => Array
        (
            [0] => action: Added
            [1] => quantity: 1
            [2] => item_code: RNA1
            [3] => product_name: Mens Organic T-shirt
            [4] => colour: White
            [5] => size: XL
        )

    [1] => Array
        (
            [6] => action: Subtracted
            [7] => quantity: 7
            [8] => item_code: RNC1
            [9] => product_name: Kids Basic T-shirt
            [10] => colour: Denim Blue
            [11] => size: 3-4y
        )

    [2] => Array
        (
            [12] => action: Added
            [13] => quantity: 20
            [14] => item_code: RNV1
            [15] => product_name: Gift Voucher
            [16] => style: Mens
            [17] => value: £20
        )

)

Here, I thought I should change the array into an associative key-value pair array, and thought that it would be relatively simple if I could find a way to convert the array into said associative array by splitting it by a character, in this case ":".

In the end, i'd like the array to look like:

'action'    =>   '',
'quantity'  =>   '',
'item_code'     =>   '',
'product_name'  =>   '',

...Etc.

I have tried using the array_column and the array_combine functions, but I haven't had much luck where i'm relatively new to PHP. Any help is greatly appreciated!

Loencodes
  • 29
  • 6
  • Use `explode()` with `;` delimiter to get the fields. Loop over the fields and use `explode()` with `:` delimiter to get the keyword and value. – Barmar Sep 07 '20 at 21:51
  • Pretty simple actually: https://repl.it/@FelipeMartins15/WittySelfreliantWorkspaces#main.php – felipsmartins Sep 07 '20 at 21:59

1 Answers1

0

You can use parse_str, after a search replace.

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

$arr = array_map(function($v) {
    parse_str(str_replace([': ', '; '], ['=', '&'], $v), $result);
    return $result;
}, $arr);

var_export($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',
  ),
)
Progrock
  • 7,373
  • 1
  • 19
  • 25