PHP爆炸并将其分配给多维数组

时间:2014-01-30 17:29:23

标签: php arrays multidimensional-array explode

我想爆炸两次字符串并制作一个多维数组。

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

所以print_r($data);会输出:

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

现在,如果我喜欢这样:

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

print_r($line_data);会给我这个:

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
)

但我想要实现的目标将是这样的:

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
)

这里,第一个索引应代表行号,第二个索引代表字号 如何将字符串分解为具有这样的数组?

6 个答案:

答案 0 :(得分:5)

做到:

$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>';

输出(您需要的):

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
    )

)

答案 1 :(得分:2)

首先,explode换行符的字符串。现在你有了一个数组。迭代此数组的值,并按空格迭代每个值explode。以下代码正是如此:

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

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

输出将是:

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 :(得分:1)

您可以在不使用爆炸的情况下使用此解决方案:

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

答案 3 :(得分:0)

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

答案 4 :(得分:0)

尝试

$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

答案 5 :(得分:-1)

如果您喜欢它又快又脏:

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