猿问

水平分解索引数组数据

导入数据后,我想水平分解每个子数组的数据,以空格作为分隔符。每行的部分可能在数量上有所不同。


我的数据:


#ORGNR   "123" 1

#ACCOUNT    1010    "Kassa"

#ACCOUNT    1050    "Bank"

#IB     -1 1010        1923.15

#IB     1 1050         201.42

我的脚本:


$array = file('data.txt');

print_r($array);

结果:


Array

(

    [0] => #ORGNR   "123" 1


    [1] => #ACCOUNT 1010    "Kassa"


    [2] => #ACCOUNT 1050    "Bank"


    [3] => #IB     -1 1010        1923.15


    [4] => #IB     1 1050         201.42


)

想要的结果(仅显示第一行)


Array

(

    [0] => [0 => #ORGNR, 1 => "123",2 => 1]

    // Line 2

    // Line 3

    // Line 4

    // Line 5


)


素胚勾勒不出你
浏览 169回答 4
4回答

牛魔王的故事

我相信最好的方法是 preg_split 而不是explode,因为它会处理额外的空间。$str = '#ORGNR   "123" 1#ACCOUNT    1010    "Kassa"#ACCOUNT    1050    "Bank"#IB     -1 1010        1923.15#IB     1 1050         201.42';$arr = explode("\n", $str); // you use file but it's the same thingforeach($arr as &$val){    $val = preg_split("/\s+/", $val);}var_dump($arr);上面的输出:array(5) {  [0]=>  array(3) {    [0]=>    string(6) "#ORGNR"    [1]=>    string(5) ""123""    [2]=>    string(1) "1"  }  [1]=>  array(3) {    [0]=>    string(8) "#ACCOUNT"    [1]=>    string(4) "1010"    [2]=>    string(7) ""Kassa""  }  [2]=>  array(3) {    [0]=>    string(8) "#ACCOUNT"    [1]=>    string(4) "1050"    [2]=>    string(6) ""Bank""  }  [3]=>  array(4) {    [0]=>    string(3) "#IB"    [1]=>    string(2) "-1"    [2]=>    string(4) "1010"    [3]=>    string(7) "1923.15"  }  [4]=>  &array(4) {    [0]=>    string(3) "#IB"    [1]=>    string(1) "1"    [2]=>    string(4) "1050"    [3]=>    string(6) "201.42"  }}https://3v4l.org/9hPVd

www说

$string = '#ORGNR&nbsp; &nbsp;"123" 1#ACCOUNT&nbsp; &nbsp; 1010&nbsp; &nbsp; "Kassa"#ACCOUNT&nbsp; &nbsp; 1050&nbsp; &nbsp; "Bank"#IB&nbsp; &nbsp; &nbsp;-1 1010&nbsp; &nbsp; &nbsp; &nbsp; 1923.15#IB&nbsp; &nbsp; &nbsp;1 1050&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;201.42';$result = explode(PHP_EOL, $string);foreach ($result as $k => $v) {&nbsp; &nbsp; $result[$k] = preg_split('/\s+/', trim($v));}echo '<pre>';print_r($result);&nbsp;&nbsp;或者$string = '#ORGNR&nbsp; &nbsp;"123" 1#ACCOUNT&nbsp; &nbsp; 1010&nbsp; &nbsp; "Kassa"#ACCOUNT&nbsp; &nbsp; 1050&nbsp; &nbsp; "Bank"#IB&nbsp; &nbsp; &nbsp;-1 1010&nbsp; &nbsp; &nbsp; &nbsp; 1923.15#IB&nbsp; &nbsp; &nbsp;1 1050&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;201.42';echo($string);$result = explode(PHP_EOL, $string);foreach ($result as $k => $v) {&nbsp; &nbsp; $result[$k] = explode(' ', trim(preg_replace('/\s+/', ' ', $v)));}echo '<pre>';print_r($result);结果[&nbsp; &nbsp; [0] => 'Array',&nbsp; &nbsp; [&nbsp; &nbsp; &nbsp; &nbsp; [0] => '#ORGNR',&nbsp; &nbsp; &nbsp; &nbsp; [1] => '"123"',&nbsp; &nbsp; &nbsp; &nbsp; [2] => '1',&nbsp; &nbsp; ]&nbsp; &nbsp; [1] => 'Array',&nbsp; &nbsp; [&nbsp; &nbsp; &nbsp; &nbsp; [0] => '#ACCOUNT',&nbsp; &nbsp; &nbsp; &nbsp; [1] => '1010',&nbsp; &nbsp; &nbsp; &nbsp; [2] => '"Kassa"',&nbsp; &nbsp; ]&nbsp; &nbsp; [2] => 'Array',&nbsp; &nbsp; [&nbsp; &nbsp; &nbsp; &nbsp; [0] => '#ACCOUNT',&nbsp; &nbsp; &nbsp; &nbsp; [1] => '1050',&nbsp; &nbsp; &nbsp; &nbsp; [2] => '"Bank"',&nbsp; &nbsp; ]&nbsp; &nbsp; [3] => 'Array',&nbsp; &nbsp; [&nbsp; &nbsp; &nbsp; &nbsp; [0] => '#IB',&nbsp; &nbsp; &nbsp; &nbsp; [1] => '-1',&nbsp; &nbsp; &nbsp; &nbsp; [2] => '1010',&nbsp; &nbsp; &nbsp; &nbsp; [3] => '1923.15',&nbsp; &nbsp; ]&nbsp; &nbsp; [4] => 'Array',&nbsp; &nbsp; [&nbsp; &nbsp; &nbsp; &nbsp; [0] => '#IB',&nbsp; &nbsp; &nbsp; &nbsp; [1] => '1',&nbsp; &nbsp; &nbsp; &nbsp; [2] => '1050',&nbsp; &nbsp; &nbsp; &nbsp; [3] => '201.42',&nbsp; &nbsp; ]]

慕姐4208626

如果我正确理解了这个问题,这就是你想做的。$dataAsArray = file('data.txt');$result = [];foreach ($dataAsArray as $data) {&nbsp; &nbsp; $result[] = explode(' ', $data);}var_dump($result);

qq_花开花谢_0

使用array_map简洁<?php$str = '#ORGNR   "123" 1#ACCOUNT    1010    "Kassa"#ACCOUNT    1050    "Bank"#IB     -1 1010        1923.15#IB     1 1050         201.42';$arr2 = array_map(function($line){    return preg_split("/\s+/", $line);}, explode("\n", $str));var_dump($arr2);将给出相同的正确输出: https: //3v4l.org/20cLd
随时随地看视频慕课网APP
我要回答