Jump to content
KrUVx

PHP, Need help with some code

Recommended Posts

Posted (edited)

$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

Share this post


Link to post
Share on other sites

What is the code that is giving you the $arr variable in that format in the first place?

Share this post


Link to post
Share on other sites
3 minutes ago, Barand said:

What is the code that is giving you the $arr variable in that format in the first place?

an api system, its an order system and thats the information it will give back

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites
2 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

thanks ill take a look into that

Share this post


Link to post
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/

Share this post


Link to post
Share on other sites

It's the first element in the array.

$str = $arr[0];

Share this post


Link to post
Share on other sites

Perhaps you you could ask the API providers to present a more user-friendly interface using JSON

EG

{"action":"Added","quantity":"1","item_code":"RNA1","product_name":"Mens organic T-Shirt","colour":"white","size":"XL"}

 

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites
1 hour ago, ginerjm said:

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

The comma, though not required, was OK. It was an array separator and not a statement terminator.

Share this post


Link to post
Share on other sites

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.