Bhojendra Nepal Bhojendra Nepal - 5 months ago 7
PHP Question

exploding textarea values into multiple array

Exploding textarea value like this:

explode("\n", $input);


would result in one array.

Currently, I am storing $input in textarea like this:

a1
a2
a3

b1
b2
b3

c1
c2
c3


But I want to get multiple array which would result like this:

$test[0][0] = 'a1';
$test[0][1] = 'a2';
$test[0][2] = 'a3';

$test[1][0] = 'b1';
$test[1][1] = 'b2';
$test[1][2] = 'b3';

$test[2][0] = 'c1';
$test[2][1] = 'c2';
$test[2][2] = 'c3';


Any idea, how can I implement?

Answer

Explode by two newlines to separate your groups, explode each group by a single newline:

$result = array_map(
    function ($group) { return explode("\n", $group); },
    explode("\n\n", $input)
);