Transform elements of a string into array PHP [closed]

-2

mysql returns the following string: ["Element1", "Element2", "Element3", "Element4", "Element5", "Element6"]

I need to transform each of the words between "" into an element of an array. I tried from the preg_split and preg_match_all functions but could not get the correct regular expression.

    
asked by anonymous 18.12.2018 / 04:58

1 answer

0

If the return of your SQL is string :

$str = '["Elemento1", "Elemento2", "Elemento3", "Elemento4", "Elemento5", "Elemento6"]';

Then just use the json_decode function to generate the array :

$arr = json_decode($str);

The generated array will be:

array (
  0 => 'Elemento1',
  1 => 'Elemento2',
  2 => 'Elemento3',
  3 => 'Elemento4',
  4 => 'Elemento5',
  5 => 'Elemento6',
)

See working at Ideone

    
18.12.2018 / 12:10