Bhojendra Rauniyar
Bhojendra Rauniyar

Reputation: 85545

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?

Upvotes: 0

Views: 75

Answers (2)

deceze
deceze

Reputation: 522155

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)
);

Upvotes: 1

Roy Stijsiger
Roy Stijsiger

Reputation: 309

Something like this should work dont know if there is anicer way

$exploded_values = explode("\n", $input);

$i = 0;
$amount_of_exlodes = 0;

foreach($explodes_values as $exploded_value){
 $test[$i][] = $exploded_value;
 $amount_of_explodes ++;
 if($amount_of_explodes >= 3){
  $amount_of_explodes = 0;
  $i++;
 }

}

Upvotes: 0

Related Questions