-4

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?

1 Answers1

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