I want to explode a string twice and make a multidimensional array.
$data = "i love funny movies \n i love stackoverflow dot com \n i like rock song";
$data = explode("\n", $data);
so it print_r($data);will output:
Array
(
[0] => i love funny movies
[1] => i love stackoverflow com
[2] => i like rock song
)
now if i do this:
$line_data = explode(" ", $data); // explode $data variable by spaces.
a print_r($line_data);will give me the following:
Array
(
[0] => i
[1] => love
[2] => funny
[3] => movies
[4] =>
[5] => i
[6] => love
[7] => stackoverflow
[8] => dot
[9] => com
[10] =>
[11] => i
[12] => like
[13] => rock
[14] => song
)
but what I want to achieve will look like this:
Array
(
[0][0] => i
[0][1] => love
[0][2] => funny
[0][3] => movies
[0][4] =>
[1][5] => i
[1][6] => love
[1][7] => stackoverflow
[1][8] => dot
[1][9] => com
[1][10] =>
[2][11] => i
[2][12] => like
[2][13] => rock
[2][14] => song
)
here, the first index should represent the line number, and the second index will represent the word number.
How to blow a string to have such an array?
source
share