0

我正在尝试将结果分解并分离为 2 个不同的数组

One One_x
Two Two_xx
Three Three_xxx
Four Four_xxxx

我首先要分解断线( \n )..
然后分解空间以在数组中找到所有One
2、3、4 和One_x、Two_xx、Three_xxx、Four_xxxx在不同的数组中

我试图炸开断线

$ex = explode("\n", $numbers);

然后

foreach($ex as $number){
  $ex = explode(" ", $number);
}

但对我来说似乎有点困惑,

如何解决这个问题?

4

2 回答 2

3
$array1 = array();
$array2 = array();
foreach($ex as $number){
  $tmp = explode(" ", $number);
  $array1[] = $tmp[0];
  $array2[] = $tmp[1];
}

我认为最简单的方法

编辑:关心你设置$ex循环......不是一个好主意。使用另一个变量

于 2012-08-12T14:01:45.840 回答
1

正则表达式可以一次拆分输入,并在其上声明结构,而不是 runnig 爆炸一次或两次:

 preg_match_all('/
         ^         # line start
         (\w+)     # One, Two, ...
         \s+       # spaces
         (\w+)     # Four_xxxxx
         $         # line end
      /smix',
      $input,
      $array
 );
 print_r($array);

会给你:

[0] => Array
    (
        [0] => One One_x
        [1] => Two Two_xx
        [2] => Three Three_xxx
        [3] => Four Four_xxxx
    )

[1] => Array
    (
        [0] => One
        [1] => Two
        [2] => Three
        [3] => Four
    )

[2] => Array
    (
        [0] => One_x
        [1] => Two_xx
        [2] => Three_xxx
        [3] => Four_xxxx
    )

也可以使用PREG_SET_ORDER甚至提取尾随的 _xxxx (?) 数字。

于 2012-08-12T14:01:48.350 回答