PHP explode and assign it to a multi-dimensional array

£可爱£侵袭症+ 提交于 2020-05-09 04:49:29

问题


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?


回答1:


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
    )

)



回答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
        )

)



回答3:


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



回答4:


$line_data = array();
foreach($data as $key => $d){
  $line_data[$key] = explode(' ', $d);
}



回答5:


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




回答6:


If you like it quick and dirty:

foreach($data = explode("\n",$data) as &$d) {
    $d = explode(' ',$d);
}


来源:https://stackoverflow.com/questions/21463420/php-explode-and-assign-it-to-a-multi-dimensional-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!