将非结构化字符串从PHP转换为2d数组


To Convert an unstructured string to 2d array om PHP

我已经将一些数据从文件解析为字符串变量,

 CPU    NFS   CIFS   HTTP   Total     Net   kB/s     HDD   kB/s     SSD   kB/s    Tape   kB/s  Cache  Cache    CP  CP   HDD   SSD   OTHER    FCP  iSCSI     FCP   kB/s   iSCSI   kB/s
                                   in    out    read  write    read  write    read  write    age    hit  time  ty  util  util                            in    out      in    out
 65%      0      0      0   11357   97020   2846       0 156160       0      0       0      0   >60    100%  100%  :f   45%    0%       3      0  11354       0      0   92987      0
 67%      0      0      0   11761  100535   2943     511 161119       0      0       0      0   >60    100%  100%  :f   43%    0%       0      0  11761       0      0   96397      0
 66%      0      0      0   11911  101736   2984     276 151088       0      0       0      0   >60    100%  100%  :v   48%    0%       0      0  11911       0      0   97534      0
 56%      0      0      0   12026  102664   3094      36     24       0      0       0      0   >60    100%    1%  :     2%    0%      11      0  12015       0      0   98419      0
 81%      0      0      0   10023   85660   2317    1964 198416       0      0       0      0   >60    100%   83%  Ff   60%    0%       0      0  10023       0      0   82117      0
 67%      0      0      0   11914  101825   2993     336 152883       0      0       0      0   >60    100%  100%  :f   55%    0%       0      0  11914       0      0   97625      0
 67%      0      0      0   11526   98491   2869     256 151040       0      0       0      0   >60    100%  100%  :f   51%    0%       0      0  11526       0      0   94388      0
 66%      0      0      0   11589   99011   2931       0 143225       0      0       0      0   >60    100%  100%  :f   51%    0%       0      0  11589       0      0   94949      0
 57%      0      0      0   11869  101355   3032      56  20544       0      0       0      0   >60    100%   26%  :    10%    0%       7      0  11862       0      0   97182      0
 76%      0      0      0    9408   79189   2212    2022 122504       0      0       0      0   >60    100%   48%  Fn   38%    0%     223      0   9185       0      0   75939      0
 74%      0      0      0   10978   92981   2651     572 147078       0      0       0      0   >60    100%  100%  :f   53%    0%      19      0  10959       0      0   89095      0
 67%      0      0      0   11839  101109   2946       8 148332       0      0       0      0   >60    100%  100%  :f   56%    0%       0      0  11839       0      0   96954      0
 64%      0      0      0   11517   98413   2899     256 138248       0      0       0      0   >60    100%  100%  :f   51%    0%       0      0  11517       0      0   94355      0
 62%      0      0      0   11653   99151   2920     559 106198       0      0       0      0   >60    100%   81%  :    40%    0%      52      0  11601       0      0   95030      0
 56%      0      0      0   11765   99752   2973     577   3009       0      0       0      0   >60    100%    3%  Fn    2%    0%     100      0  11665       0      0   95652      0
 82%      0      0      0    9987   85219   2327    1570 207259       0      0       0      0   >60    100%  100%  :f   60%    0%       5      0   9982       0      0   81692      0
 67%      0      0      0   11859  101347   2970       0 158696       0      0       0      0   >60    100%  100%  :f   57%    0%       0      0  11859 

到一个二维数组中,这样我就可以取其中一列的平均值,谁能告诉我怎么做?

我代码:

preg_match('/'.preg_quote($word1).'(.*?)'.preg_quote($word2).'/is', $akshay_file, $match);
                            $text2 = nl2br($match[1]);
                            echo "$text2"; 

谁能让我知道它是怎么做的,我做错了吗?谢谢。

我猜您是从文件中读取的。作为一个选项,逐行读取文件,并考虑以下代码

<?php
$n = "65%      0      0      0   11357   97020   2846       0 156160       0      0       0      0   >60    100%  100%  :f   45%    0%       3      0  11354       0      0   92987      0";
$n = preg_replace('!'s+!', ' ', $n);
$parts = explode(' ', $n);
echo '<pre>'.print_r($parts, true).'</pre>';
?>

首先应该将代码分成几行,然后用空格将每行分开。

$lines = preg_split('/$'R?^/m', $data);
foreach ($lines as $line) {
    $cols = preg_split('/'s's+/', $data);
    // do sth..
}