dongmaijie5200
dongmaijie5200
2015-07-30 21:02
浏览 41
已采纳

如何从字符串变量创建一个php数组? [重复]

This question already has an answer here:

How to create an array from a string variable? This is the script I tried but it didn't work:

$names = '"Darius","Fiora","Katarina","Zed","Ahri","Lee sin"';
$array = array($names);
</div>

图片转代码服务由CSDN问答提供 功能建议

此问题已经存在 这里有一个答案:

  • 如何将字符串转换为php中的数组 10 answers

    如何从字符串变量创建数组? 这是我尝试过的剧本,但它不起作用:

      $ names ='“Darius”,“Fiora”,“Katarina”,“Zed”,“Ahri”  ,“Lee sin”'; 
     $ array = array($ names); 
       
     
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • doufeixuan8882
    doufeixuan8882 2015-07-30 21:07
    已采纳

    I dont think explode is what he wants, as it can add " to the elements in the array.

    If your string is pretty SURE formatted, eval function is an optional choice (more efficient):

    $names = '"Darius","Fiora","Katarina","Zed","Ahri","Lee sin"';
    eval('$array=array('.$names.');');
    print_r($array);
    

    See result

    NOTE: plz note eval is some danger if your string is not that certain, as @jay mentioned.

    点赞 评论
  • duanduo3712
    duanduo3712 2015-07-30 21:10

    You can use explode() as follow :

    $arr = explode(',',$names); 
    

    Or if you wan to do the opposite use implode().

    点赞 评论

相关推荐