将字符串传递给PHP数组

Dav*_*yre -1 php arrays

我有这样的字符串列表:

'PYRAMID','htc_europe','htc_pyramid','pyramid','pyramid','HTC','1.11.401.110 CL68035 release-keys','htc_europe/pyramid/pyramid:4.0.3/IML74K/68035.110:user /释放密钥

它看起来像数组的元素,但是当我使用时

<?php
    $string = "'PYRAMID','htc_europe','htc_pyramid','pyramid','pyramid','HTC','1.11.401.110 CL68035 release-keys','htc_europe/pyramid/pyramid:4.0.3/IML74K/68035.110:user/release-keys'";
    $arr = array($string);
    print_r($arr);

 ?>
Run Code Online (Sandbox Code Playgroud)

它不能按我的意愿工作:

数组([0] =>'PYRAMID','htc_europe','htc_pyramid','金字塔','金字塔','HTC','1.11.401.110 CL68035 release-keys','htc_europe/pyramid/pyramid:4.0. 3/IML74K/68035.110:用户/释放密钥)

代替:

数组([0] => PYRAMID,[1] => htc_europe,[2] => htc_pyramid,...

我不想使用explode(),因为我的字符串已经是数组格式,许多字符串都有' , '字符.请帮帮我,谢谢.

jer*_*oen 5

您的字符串不是数组格式.从它的外观和基于你的评论的方式,我会说你有逗号分隔值,CSV.所以解析它的最好方法是使用专门为该格式制作的函数,如str_getcsv():

$str = "'PYRAMID','htc_europe','htc_pyramid','pyramid','pyramid','HTC','1.11.401.110 CL68035 release-keys','htc_europe/pyramid/pyramid:4.0.3/IML74K/68035.110:user/release-keys'";

// this will get you the result you are looking for
$arr = str_getcsv($str, ',', "'");

var_dump($arr);
Run Code Online (Sandbox Code Playgroud)

使用第二个和第三个参数可确保在字符串包含逗号时也能正确解析它.