-2

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'      =>   '',
Loencodes
  • 29
  • 6
  • 1
    What should the resulting array look like? – Alex Barker Sep 07 '20 at 15:59
  • 1
    This is very similar to your last question [How can I convert an array of strings into an associative array in PHP?](https://stackoverflow.com/questions/63742901/how-can-i-convert-an-array-of-strings-into-an-associative-array-in-php) which you seem to have accepted an answer for. Is there a problem with the answer you accepted? – Nigel Ren Sep 07 '20 at 16:04
  • Sorry about the repost, I reposted because I would appreciate some explanations on how the code functions similarly to how i've commented on my own code. – Loencodes Sep 07 '20 at 16:11
  • Explanations of __what exactly__? – u_mulder Sep 07 '20 at 16:21

2 Answers2

1

Is this what you need?

<?php

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

foreach ($arr as $string) {
   //Build array
   preg_match_all("/ [ ]?([^:]+): ([^;]+)[ ;]? /x", $string, $p);
   $array = array_combine($p[1], $p[2]);

   //Print it or do something else with it
    print_r($array);
}

https://3v4l.org/pTJDj

Chaz
  • 672
  • 1
  • 5
  • 19
  • 1
    Isn't this the same answer - https://stackoverflow.com/a/63743439/1213708 – Nigel Ren Sep 07 '20 at 16:04
  • yes it is, just flagged it as duplicate, wasn't the accepted answer there though, maybe op had a problem with the accepted answer and just reposted the question – Chaz Sep 07 '20 at 16:06
1

Use array_combine.

$keyValue = array();

$len = count($array);

for($i=1; $i<$len; $i++) {
    $keyValue[] = array_combine($array[0], $array[$i]);
}

Reference: How to convert an array into key-value pair array?

Swatantra Kumar
  • 1,324
  • 5
  • 24
  • 32