4

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

$data = "i love funny movies \n i love stackoverflow 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 love stackoverflow 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] => love
    [7] => stackoverflow
    [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] => 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 second index will represent the word number.
How to explode the string to have such array?

rakibtg
  • 5,521
  • 11
  • 50
  • 73

6 Answers6

7

Do it:

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

$out = array();
$step = 0;
$last = count($data);
$last--;

foreach($data as $key=>$item){

   foreach(explode(' ',$item) as $value){
    $out[$key][$step++] = $value;
   }

   if ($key!=$last){
    $out[$key][$step++] = ' '; // not inserting last "space"
   }

}

print '<pre>';
print_r($out);
print '</pre>';

OUTPUT (what you need):

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

[1] => Array
    (
        [5] => i
        [6] => love
        [7] => stackoverflow
        [8] => dot
        [9] => com
        [10] =>  
    )

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

)
Sharanya Dutta
  • 3,981
  • 2
  • 17
  • 27
voodoo417
  • 11,861
  • 3
  • 36
  • 40
  • The $step variable is useless, you can append to the end of the array with this notation: `$out[$key][] = $value;` – Patrick Sep 11 '18 at 08:35
  • @cornegigouille Bro, suspect, U haven`t got this moment. Try plz under debug/trace/etc. by step to see how it works and "why there is no just $out[$key][] = $value;". Cheers!) – voodoo417 Dec 04 '18 at 10:20
2

First, explode the string by newline. Now you have an array. Iterate over the values of this array and explode each value by space. The following code does exactly this:

$data = "i love funny movies \n i love stackoverflow dot com \n i like rock song";
$data = explode("\n ", $data);
$num = 0;
foreach($data as &$val){
$exp = explode(" ", $val);
$val = array_combine(range($num, $num+sizeof($exp)-1), $exp);
$num += sizeof($exp);
}
print_r($data);

Alternative solution with array_search and array_slice:

$data = "i love funny movies \n i love stackoverflow dot com \n i like rock song";
$data = explode(" ", $data);
$arr = array();
while($search = array_search("\n", array_values($data))){
$arr[] = array_merge(array_slice($data, 0, $search, true), array(""));
$data = array_slice($data, $search+1, null, true);
}
$data = array_merge($arr, array($data));
print_r($data);

The output will be:

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

    [1] => Array
        (
            [5] => i
            [6] => love
            [7] => stackoverflow
            [8] => dot
            [9] => com
            [10] => 
        )

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

)
Sharanya Dutta
  • 3,981
  • 2
  • 17
  • 27
1

Try

$data = "i love funny movies \n i love stackoverflow dot com \n i like rock song";
$data = explode("\n", $data);
for($i=0;$i<count($data);$i++){
 $data[$i] = explode(" ",$data[$i]);
}

demo here

Nouphal.M
  • 6,304
  • 1
  • 17
  • 28
1

You can use this solution without using explode:

   $data = "i love funny movies \n i love stackoverflow dot com \n i like rock song";

   $line_data = explode(" ", $data);

   $arrayLines = [];

   for($i = 0, $j = 0; $i < count($line_data); $i++) {
     if($line_data[$i] != "\n") {
      $arrayLines[$j][$i] = $line_data[$i];
     } else {
       $arrayLines[$j][$i] = $line_data[$i];
       $j++;
     }
   }

   print_r($arrayLines);
Lucas Reis
  • 441
  • 2
  • 9
1

If you like it quick and dirty:

foreach($data = explode("\n",$data) as &$d) {
    $d = explode(' ',$d);
}
cronoklee
  • 6,482
  • 9
  • 52
  • 80
0
$line_data = array();
foreach($data as $key => $d){
  $line_data[$key] = explode(' ', $d);
}
Zolyboy
  • 457
  • 7
  • 17