PHP explode and assign it to a multi-dimensional array

前端 未结 6 1893
天命终不由人
天命终不由人 2021-01-19 17:11

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\"         


        
6条回答
  •  说谎
    说谎 (楼主)
    2021-01-19 17:44

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

提交回复
热议问题