[英]PHP convert comma separated string with square brackets into array
My string looks like: 我的字符串看起来像:
[10,20,30] [10,20,30]
I want to convert it to an array. 我想将其转换为数组。
I've tried: 我试过了:
$myArray=explode(",",$myString);
print_r($myArray);
But this is returning: 但这正在返回:
Array ( [0] => [10 [1] => 20 [2] => 30] )
I need to get rid of the opening/closing brackets. 我需要摆脱开/关括号。
Can someone help? 有人可以帮忙吗?
特定格式的数字数组是有效的JSON,因此您可以使用PHP的内置函数 :
$myArray = json_decode($myString);
I think you can remove the square brackets first with str_replace function. 我认为您可以先使用str_replace函数删除方括号。 Then you can simply do the rest. 然后,您可以简单地做剩下的事情。 This will work I think. 我认为这将起作用。
$inputString = "[10,20,30]";
$processString = str_replace(array('[',']') ,'' , $inputString);
$outputArray = explode(',' , $processString);
var_dump($outputArray);
//output:
//array(3) { [0]=> string(2) "10" [1]=> string(2) "20" [2]=> string(2) "30" }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.