0

i want to explode a string two time and make a multi-dimensional array.

$data = "i love funny movies \n i like daniweb dot com \n i like rock song";
$data = explode("\n", $data);

so a print_r($data); will output:

Array
(
    [0] => i love funny movies 
    [1] =>  i like daniweb dot com 
    [2] =>  i like rock song
)

now if i do like this:

$line_data = explode(" ", $data); // explode $data variable by spaces.

a print_r($line_data); will give me this:

Array
(
    [0] => i
    [1] => love
    [2] => funny
    [3] => movies
    [4] => 

    [5] => i
    [6] => like
    [7] => daniweb
    [8] => dot
    [9] => com
    [10] => 

    [11] => i
    [12] => like
    [13] => rock
    [14] => song
)

but what i want to achive will look like this:

Array
(
    [0][0] => i
    [0][1] => love
    [0][2] => funny
    [0][3] => movies
    [0][4] => 

    [1][5] => i
    [1][6] => like
    [1][7] => daniweb
    [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 second index will represent the word number.
How to explode the string to have such array?
Thankyou everyone.

2
Contributors
1
Reply
20
Views
3 Years
Discussion Span
Last Post by pritaeas
0
$data = "i love funny movies \n i like daniweb dot com \n i like rock song";
$data = explode("\n", $data);

$data2 = array ();
foreach ($data as $item)
{
    $data2[] = explode(" ", $item);
}

print_r($data2);
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.