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?
3
-
Does this answer your question? Array to String PHP?stud3nt– stud3nt2019-12-04 07:58:39 +00:00Commented Dec 4, 2019 at 7:58
-
Does this answer your question? Split PHP Variable in to arrayMarkus Zeller– Markus Zeller2019-12-04 07:59:22 +00:00Commented Dec 4, 2019 at 7:59
-
@Nick yep that's implode()Anitha– Anitha2019-12-04 08:26:25 +00:00Commented Dec 4, 2019 at 8:26
Add a comment
|
1 Answer
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"
}