0

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?

5
  • What does the $input look like? What format is it in? Commented Jun 29, 2016 at 10:04
  • What's the input? Based on what rule do you expect to construct that result? Commented Jun 29, 2016 at 10:04
  • In what format? is it a1,a2,a3\nb1,b2,b3\nc1,c2,c3 for example? Commented Jun 29, 2016 at 10:14
  • 1
    With 32k reputation I'd expect you to be able to formulate better questions, and to see why this question is incomprehensible/doesn't contain all the information needed to answer it. Commented Jun 29, 2016 at 10:14
  • should be clear now. Commented Jun 29, 2016 at 10:17

2 Answers 2

1

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)
);
Sign up to request clarification or add additional context in comments.

6 Comments

amazed:) I'm getting like this: Array ([0] => Array ([0] => a [1] => b [2] => c [3] => [4] => d [5] => e [6] => f [7] => [8] => g [9] => h [10] => i ) )
Then your input is actually different. Are newlines \r\n perhaps...?
Any other idea? I would be glad if I can separate them by a group name in textarea like: group1 a1 a2 a3 group2 b1 b2 b3 etc. each are in new line.
Lot's of possible ways to skin this cat. a1,a2,a3 \n b1,b2,..., or whatever other format you can come up with. What's the easiest to write for the people needing to input this date...?!
Yeah agree. Just wanted to know using group name.
|
0

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++;
 }

}

3 Comments

Well he wants a new array inside an array every 3 times right.. This might work I ahvent tested it but hey.. And it probably isnt the nicest code but it wil work.
"he wants a new array inside an array every 3 times right" – that's exactly what we don't know here... Could be some completely different logic.
According to his example I think thats what he wants, then this works otherwise he should make himself more clear. im trying to help as much people to actually get some reputation so i have more priviliges

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.