PHP explode and assign it to a multi-dimensional array

前端 未结 6 1894
天命终不由人
天命终不由人 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:31

    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
            )
    
    )
    
    0 讨论(0)
  • 2021-01-19 17:35

    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
        )
    
    )
    
    0 讨论(0)
  • 2021-01-19 17:38
    $line_data = array();
    foreach($data as $key => $d){
      $line_data[$key] = explode(' ', $d);
    }
    
    0 讨论(0)
  • 2021-01-19 17:38

    If you like it quick and dirty:

    foreach($data = explode("\n",$data) as &$d) {
        $d = explode(' ',$d);
    }
    
    0 讨论(0)
  • 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);
    
    0 讨论(0)
  • 2021-01-19 17:45

    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

    0 讨论(0)
提交回复
热议问题