KrUVx Posted June 24, 2019 Share Posted June 24, 2019 (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 June 24, 2019 by KrUVx Quote Link to comment Share on other sites More sharing options...
Barand Posted June 24, 2019 Share Posted June 24, 2019 What is the code that is giving you the $arr variable in that format in the first place? Quote Link to comment Share on other sites More sharing options...
KrUVx Posted June 24, 2019 Author Share Posted June 24, 2019 an api system, its an order from a client Quote Link to comment Share on other sites More sharing options...
KrUVx Posted June 24, 2019 Author Share Posted June 24, 2019 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 Quote Link to comment Share on other sites More sharing options...
Barand Posted June 24, 2019 Share Posted June 24, 2019 explode() the string on the ";" to get 6 sections, then explode() each section on the ":" to get get keys and values Quote Link to comment Share on other sites More sharing options...
KrUVx Posted June 24, 2019 Author Share Posted June 24, 2019 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 Quote Link to comment Share on other sites More sharing options...
KrUVx Posted June 24, 2019 Author Share Posted June 24, 2019 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/ Quote Link to comment Share on other sites More sharing options...
Barand Posted June 24, 2019 Share Posted June 24, 2019 It's the first element in the array. $str = $arr[0]; Quote Link to comment Share on other sites More sharing options...
Barand Posted June 24, 2019 Share Posted June 24, 2019 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"} Quote Link to comment Share on other sites More sharing options...
ginerjm Posted June 24, 2019 Share Posted June 24, 2019 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. Quote Link to comment Share on other sites More sharing options...
Barand Posted June 24, 2019 Share Posted June 24, 2019 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.