PHP将格式化字符串(标题+值)转换为多维数组

时间:2017-08-29 09:29:59

标签: php arrays multidimensional-array

我想将这个格式化的字符串转换为多维数组,但我无法做到。

目标是提取标题标题( Dreg Time,FN,SS_ID,MAC,Sector ID,Type,Reason,CMPNT,Basic,Cid )并关联相应的值。

因为在最终结果中,我需要处理“原因”列中的数据。

字符串:

$log = ": mss get dereg_log 2 500



---------- MODEM 2 MAC ---------



Dreg Time                   FN       SS_ID     MAC           Sector ID Type Reason CMPNT Basic Cid

2017/08/29 08:20:39:627   0x4da9f0  8277 00:24:a0:xx:xx:xx         1    2     25     0        86

2017/08/29 07:17:33:478   0x421c02  8238 00:23:a2:xx:xx:xx         1    3     59     0        47

2017/08/29 06:00:43:232   0x340a41  8508 00:23:a2:xx:xx:xx         1    1      6    13       317



Total Deregistrations since sector active in sector 1: 9576

Derigistrations since dreg log last reset in sector 1: 9576



Deregistration Types:

    DEREG_MSS_IMMEDIATE = 0

    DEREG_MSS_DREG_REQ_FROM_AP = 1

    DEREG_MSS_DREG_REQ_FROM_MS = 2

    DEREG_MSS_RNG_RSP_ABORT_FROM_AP = 3

    DEREG_MSS_CONTACT_LOST = 4



---------------------------------

2017-Aug-29 08:31:53.860";

所需数组:

Array
(
    [0] => Array
        (
            [Dreg Time] => '2017/08/29 08:20:39:627'
            [FN] => '0x4da9f0'
            [SS_ID] => '8277'
            [MAC] => '00:24:a0:xx:xx:xx'
            [Sector ID] => '1'
            [Type] => '2'
            [Reason] => '25'
            [CMPNT] => '0'
            [Basic Cid] => '86'
        )

    [1] => Array
        (
            [Dreg Time] => '2017/08/29 07:17:33:478'
            [FN] => '0x421c02'
            [SS_ID] => '8238'
            [MAC] => '00:23:a2:xx:xx:xx'
            [Sector ID] => '1'
            [Type] => '3'
            [Reason] => '59'
            [CMPNT] => '0'
            [Basic Cid] => '47'
        )

    [2] => Array
        (
            [Dreg Time] => '2017/08/29 06:00:43:232'
            [FN] => '0x340a41'
            [SS_ID] => '8508'
            [MAC] => '00:23:a2:xx:xx:xx'
            [Sector ID] => '1'
            [Type] => '1'
            [Reason] => '6'
            [CMPNT] => '13'
            [Basic Cid] => '317'
        )
)

代码尝试:

echo '<pre>';
// remove blank lines
$log = preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", "\n", $log);
// remove new lines
$log = preg_replace('/^.+\n.+\n/', '', $log);

$lines = explode( "\n\n", $log );
$return = array();

foreach ($lines as $line) {
  $items = explode("\n", $line);
  $return[array_shift($items)] = $items;
}

print_r($return);

结果:

Array
(
    [Dreg Time                   FN       SS_ID     MAC           Sector ID Type Reason CMPNT Basic Cid] => Array
        (
            [0] => 2017/08/29 08:20:39:627   0x4da9f0  8277 00:24:a0:xx:xx:xx         1    2     25     0        86
            [1] => 2017/08/29 07:17:33:478   0x421c02  8238 00:23:a2:xx:xx:xx         1    3     59     0        47
            [2] => 2017/08/29 06:00:43:232   0x340a41  8508 00:23:a2:xx:xx:xx         1    1      6    13       317
            [3] => Total Deregistrations since sector active in sector 1: 9576
            [4] => Derigistrations since dreg log last reset in sector 1: 9576
            [5] => Deregistration Types:
            [6] =>     DEREG_MSS_IMMEDIATE = 0
            [7] =>     DEREG_MSS_DREG_REQ_FROM_AP = 1
            [8] =>     DEREG_MSS_DREG_REQ_FROM_MS = 2
            [9] =>     DEREG_MSS_RNG_RSP_ABORT_FROM_AP = 3
            [10] =>     DEREG_MSS_CONTACT_LOST = 4
            [11] => ---------------------------------
            [12] => 2017-Aug-29 08:31:53.860
        )

)

1 个答案:

答案 0 :(得分:1)

您的代码是一次不完整的尝试,并包含一些错误 - 例如您从日志中删除了换行符,然后尝试根据换行符拆分字符串!然后你尝试用换行符拆分一行,显然单行不能包含换行符。此外,代码只是天真地遍历每一行并将其直接添加到数组中,而不是在每个索引中创建一个关联数组,如所需的输出所示。最后,它没有尝试确定哪些行实际包含所需的输出。

这样做:

$log = preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", "\n", $log);
$lines = explode( "\n", $log );
$return = array();

foreach ($lines as $line) {
  $line = preg_replace("/\s+/", " ", $line); //condense and standardise the whitespace between each item, so that explode can work.
  $items = explode(" ", $line);
  if (count($items) == 10 && preg_match("/^[0-9]{4}/", $items[0])) //restrict to lines starting with the year, and with the correct number of columns
      $return[] = array(
            "Dreg Time" => $items[0]." ".$items[1],
            "FN" => $items[2],
            "SS_ID" => $items[3],
            "MAC" => $items[4],
            "Sector_ID" => $items[5],
            "Type" => $items[6],
            "Reason" => $items[7],
            "CMPNT" => $items[8],
            "Basic_Cid" => $items[9]
      );
}

 echo "<pre>".var_export($return, true)."</pre>";
相关问题