PHP将句子转换为数组?

时间:2015-07-24 09:37:09

标签: php arrays

我的数据返回如下:

2015-07-2111
ali,21,malaysia
alan,23,england

我预期的数组:

0=>2015-07-2111
1=>ali,21,malaysia
2=>alan,23,england

我的代码:

$info = explode(",",$string);

但代码不会产生我想要的数组。

2 个答案:

答案 0 :(得分:1)

当你应该逐行拆分时,你将它,拆分,所以改变

$info = explode(",", $string);
//where $info becomes an array of "ali", "21", "malaysia\nalan", "23", "england"

$info = explode(PHP_EOL, $string);
//where $info becomes an array of "ali,21,malaysia", "alan,23,england"

PHP_EOL是“correct 'End Of Line' symbol for this platform

答案 1 :(得分:0)

您可以使用

$str =  "2015-07-2111
ali,21,malaysia
alan,23,england";

echo '<pre>';
print_r(explode("\n",$str));