I have some data like: oil dispenser, oil container, oil spray bottle, oil Bottle And I want it to become: ["oil dispenser", "oil container", "oil spray bottle", "oil Bottle"] How can I convert a string into an array?
Asked
Active
Viewed 68 times
-4
-
Does this answer your question? [Array to String PHP?](https://stackoverflow.com/questions/7490488/array-to-string-php) – stud3nt Dec 04 '19 at 07:58
-
Does this answer your question? [Split PHP Variable in to array](https://stackoverflow.com/questions/17755910/split-php-variable-in-to-array) – Markus Zeller Dec 04 '19 at 07:59
-
@Nick yep that's implode() – Anitha Dec 04 '19 at 08:26
1 Answers
1
Try this:
Solution
$string = 'oil dispenser, oil container, oil spray bottle, oil Bottle';
$array = array_map('trim', explode(',', $string));
var_dump($array);
Output
array(4) {
[0]=>
string(13) "oil dispenser"
[1]=>
string(13) "oil container"
[2]=>
string(16) "oil spray bottle"
[3]=>
string(10) "oil Bottle"
}

Bluetree
- 1,324
- 2
- 8
- 25