Jump to content

PHP, Need help with some code


KrUVx

Recommended Posts

$arr = array(

 

 “action: Added; quantity: 1; item_code: RNA1; product_name: Mens organic T-Shirt; colour: white; size: XL”,

);

 

[Code i need is to take apart the string that is above and display it below]

array(

  'action' => 'Added',

  'quantity' => 1,

  ' item_code' => RNA1,

  'product_name' => 'Mens Organic T-shirt',

  'colour' => 'White',

  'size' => 'XL'

)

Edited by KrUVx
Link to comment
Share on other sites

9 minutes ago, Barand said:

explode() the string on the ";" to get 6 sections, then explode() each section on the ":" to get get keys and values

how would i turn the original array into a string to explode() it/

Link to comment
Share on other sites

If you are stuck with the current format, here is one way to handle it.

	$input_str = "action: Added; quantity: 1; item_code: RNA1; product_name: Mens organic T-Shirt; colour: white; size: XL";
$items = explode(';',$input_str);
foreach($items as $item)
{
    list($k,$v) = explode(':',$item);
    $k = trim($k);
    $v = trim($v);
    $final_arr[$k] = $v;
}
echo "final result: <pre>",print_r($final_arr,true),"</pre>";
exit();
	

Note: the comma at the end of your original input sample is possible incorrect.  I replaced it with a semi.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.