AFN AFN - 2 months ago 41
PHP Question

Php - convert string to array

I have an array in string like this :

"[14,16,18]"


I want to remove double quotation and my result would be like :

[14,16,18]


I've tried :

$restaurantIds =Request::All();
dd(trim(array_values($restaurantIds)[0], '"'),array_values($restaurantIds)[0] , $restaurantIds) ;


And Result is :

"[14,16,18]"
"[14,16,18]"
array:1 [
"restaurant" => "[14,16,18]"
]


Any suggestion?

Answer Source

You can do it like below:-

<?php

$string = "[14,16,18]";

$array = json_decode($string, true);
print_r($array);

Output:- https://eval.in/847737

Another solution:-

<?php

$string = "[14,16,18]";

$array = explode(',',str_replace(array('[',']'),array('',''),$string));

print_r($array);

Output:-https://eval.in/847734