0

How would I take the top array and convert the objects into the bottom aspect?


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

);





array(
    'action'    =>   '',
    'quantity'  =>   '',
    'item_code'     =>   '',
    'product_name'  =>   '',
    'colour'    =>   '',
    'size'      =>   '',
);

My friend has asked me to help with this but I'm not very familiar with this area.

1
  • What have you tried so far? Where are you stuck? Commented Jun 6, 2020 at 11:02

2 Answers 2

2

Like this you could:

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

);

$outputarray = [];

foreach($arr as $key => $line){
  $items = explode(';', $line);
  foreach($items as $item){
      $property = trim(explode(':', $item)[0]);
      $value = trim(explode(':', $item)[1]);
      $outputarray[$key][$property] = $value;
  }
}

print_r($outputarray);

array(
    'action'    =>   '',
    'quantity'  =>   '',
    'item_code'     =>   '',
    'product_name'  =>   '',
    'colour'    =>   '',
    'size'      =>   '',
);

Is that ok?

Output of $outputarray:

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 ) )
Sign up to request clarification or add additional context in comments.

2 Comments

Structure or content ? Do you mean add quotation marks? Or the structure? Feel free to improve it
While this code may solve the question, including an explanation of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please edit your answer to add explanations and give an indication of what limitations and assumptions apply.
2

One way to do it is by finding attributes and values in the strings using a regular expression, then merging them into key-value pairs with array_combine.

$items = array_map(function($item) {
    preg_match_all('/(\w+): ([^;]+)/', $item, $attributes);
    return array_combine($attributes[1], $attributes[2]);
}, $arr);

2 Comments

Shorter :) nice
Reminds me of myself (if/foreach) everything in codewars.com. Then someone has a genius oneliner, really learning from that:)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.