将逗号分隔的字符串拆分为平面数组,而不分隔双引号子字符串[重复]

问题描述 投票:0回答:1

如何分解这个数组值:

$row = array(
[0] = 1,1,Carlyle,Rogers,1,"Carlyle, Rogers",0000-00-00,,[email protected],,non premium)

我试过这个代码

$values         =   explode(',', $row[0]);

并给我这个错误的输出:

Array (
   [0] => 1
   [1] => 1
   [2] => Carlyle
   [3] => Rogers
   [4] => 1
   [5] => "Carlyle
   [6] => Rogers"
   [7] => 0000-00-00
   [8] => 
   [9] => [email protected]
   [10] => 
   [11] => non premium
)

我想要的是输出必须是这样的:

Array (
   [0] => 1
   [1] => 1
   [2] => Carlyle
   [3] => Rogers
   [4] => 1
   [5] => "Carlyle, Rogers"
   [6] => 0000-00-00
   [7] => 
   [8] => [email protected]
   [9] => 
   [10] => non premium
)
php arrays csv split delimited
1个回答
5
投票

您不能像这样使用

explode
,因为您的输入似乎是CSV格式,而
explode
对这种“格式”一无所知。使用
str_getcsv
代替:

$values = str_getcsv($row[0]);
© www.soinside.com 2019 - 2024. All rights reserved.