max cheng
max cheng

Reputation: 181

PHP convert sentence to array?

i have data return as below:

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

my expected array:

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

my code :

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

But the code do not produce the array that i want.

Upvotes: 0

Views: 197

Answers (2)

Sunil Pachlangia
Sunil Pachlangia

Reputation: 2071

You can use

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

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

Upvotes: 0

Ben
Ben

Reputation: 9001

You're splitting it by , when you should be splitting it line by line, so change

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

to

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

as PHP_EOL is the "correct 'End Of Line' symbol for this platform"

Upvotes: 1

Related Questions