PHP-将元素过滤到一个单独的多维数组中,该数组附加在前一个键上



我可能会用完全错误的方式来处理这个问题,但我想我应该先在这里提出这个问题。我有一个从Windows进程RoboCopy生成的日志文件,然后我通过PHP打开它,并为日志的每一行创建一个数组。然后我过滤了整个阵列,只提取出我需要的行:

[0] => Started : 25 June 2015 13:12:48
[1] => 2015/06/25 13:13:46 ERROR 53 (0x00000035) Getting File System Type of Destination \test-lape$test
[2] => 2015/06/25 13:13:46 ERROR 53 (0x00000035) Accessing Destination Directory \test-lape$test
[3] => modified        4.7 m    2015-Map.pdf
[4] => modified        2.6 m    B7x0_ES71x0_PCL6_x86_ENU_2201_tcm3-154780.exe
[5] => modified        2.6 m    B7x0_ES71x0_PCL_x86_ENU_2201_tcm3-154696.exe
[6] => modified        2.7 m    B7x0_ES71x0_PS_x86_ENU_2201_tcm3-154863.exe
[7] => modified       45.7 m    V22.01.07.zip
[8] => modified       14.8 m    Chipset_Driver_H1VVY_WN_6.2.8400.39034_A03.EXE
[9] => modified       21.3 m    Chipset_Driver_M6X6G_WN_3.0.0.16_A00.EXE
[10] => modified            1100    cpaddin_example.ini
[11] => modified            1391    ddict.txt
[12] => modified           1.2 m    desktop.jpg
[13] => modified           5.0 m    EPS_uninstall_tool.exe
[14] => modified          741819    Install.log
[15] => modified          15.0 m    Network_Driver_64ND2_WN_7.80.218.2014_A00.EXE
[16] => modified            8746    New Microsoft Excel Worksheet.xlsx
[17] => modified               0    New Microsoft Visio Drawing.vsd
[18] => modified               0    New Microsoft Word Document (2).docx
[19] => modified               0    New Microsoft Word Document (3).docx
[20] => modified               0    New Microsoft Word Document.docx
[21] => modified               0    New Text Document.txt
[22] => modified           1.8 m    OkiC5x50_ES2x32a4PCLx64GB_tcm3-37365.exe
[23] => modified           1.8 m    OkiC5x50_ES2x32a4PCLx86GB_tcm3-37451.exe
[24] => Newer              9.8 m    OKW3E04V101_tcm3-155664.exe
[25] => 0.0%
[26] => 2015/06/25 13:18:22 ERROR 53 (0x00000035) Copying File c:testOKW3E04V101_tcm3-155664.exe
[27] => Newer              9.8 m    OKW3E04V101_tcm3-155664.exe
[28] => 2015/06/25 13:22:00 ERROR 53 (0x00000035) Copying File c:testOKW3E04V101_tcm3-155664.exe
[29] => Newer              9.8 m    OKW3E04V101_tcm3-155664.exe
[30] => 0.0%
[31] => 10.1%
[32] => 20.3%
[33] => 30.5%
[34] => 40.6%
[35] => 50.8%
[36] => 61.0%
[37] => 71.1%
[38] => 81.3%
[39] => 91.5%
[40] => 100%
[41] => 100%
[42] => 100%
[43] => New File           1.6 m    PSTools.zip 13:37 -> 13:41
[44] => 0%
[45] => 2015/06/25 13:45:17 ERROR 121 (0x00000079) Copying File c:testPSTools.zip
[46] => New File           1.6 m    PSTools.zip 13:45 -> 13:50
[47] => 0%
[48] => 15%
[49] => 31%
[50] => 46%
[51] => 62%
[52] => 77%
[53] => 93%
[54] => 99%
[55] => 99%
[56] => 100%

我试图从这里做的是,每当显示百分比时,我想取消设置该元素,并将其作为一个单独的数组附加到上一个元素。因此,对于报告的每个文件,我最终都会得到这样的结果:

[0] => Started : 25 June 2015 13:12:48
[1] => 2015/06/25 13:13:46 ERROR 53 (0x00000035) Getting File System Type of Destination \test-laptope$test
[2] => 2015/06/25 13:13:46 ERROR 53 (0x00000035) Accessing Destination Directory \test-laptope$test
[3] => modified        4.7 m    2015-Map.pdf
[4] => modified        2.6 m    B7x0_ES71x0_PCL6_x86_ENU_2201_tcm3-154780.exe
[5] => modified        2.6 m    B7x0_ES71x0_PCL_x86_ENU_2201_tcm3-154696.exe
[6] => modified        2.7 m    B7x0_ES71x0_PS_x86_ENU_2201_tcm3-154863.exe
[7] => modified       45.7 m    V22.01.07.zip
[8] => modified       14.8 m    Chipset_Driver_H1VVY_WN_6.2.8400.39034_A03.EXE
[9] => modified       21.3 m    Chipset_Driver_M6X6G_WN_3.0.0.16_A00.EXE
[10] => modified            1100    cpaddin_example.ini
[11] => modified            1391    ddict.txt
[12] => modified           1.2 m    desktop.jpg
[13] => modified           5.0 m    EPS_uninstall_tool.exe
[14] => modified          741819    Install.log
[15] => modified          15.0 m    Network_Driver_64ND2_WN_7.80.218.2014_A00.EXE
[16] => modified            8746    New Microsoft Excel Worksheet.xlsx
[17] => modified               0    New Microsoft Visio Drawing.vsd
[18] => modified               0    New Microsoft Word Document (2).docx
[19] => modified               0    New Microsoft Word Document (3).docx
[20] => modified               0    New Microsoft Word Document.docx
[21] => modified               0    New Text Document.txt
[22] => modified           1.8 m    OkiC5x50_ES2x32a4PCLx64GB_tcm3-37365.exe
[23] => modified           1.8 m    OkiC5x50_ES2x32a4PCLx86GB_tcm3-37451.exe
[24] => Array
    [0] Newer              9.8 m    OKW3E04V101_tcm3-155664.exe
    [1] => 0.0%
[25] => 2015/06/25 13:18:22 ERROR 53 (0x00000035) Copying File c:testOKW3E04V101_tcm3-155664.exe
[26] => Newer              9.8 m    OKW3E04V101_tcm3-155664.exe
[27] => 2015/06/25 13:22:00 ERROR 53 (0x00000035) Copying File c:testOKW3E04V101_tcm3-155664.exe
[28] => Array
    [0] => Newer               9.8 m    OKW3E04V101_tcm3-155664.exe
    [1] => 0.0%
    [2] => 10.1%
    [3] => 20.3%
    [4] => 30.5%
    [5] => 40.6%
    [6] => 50.8%
    [7] => 61.0%
    [8] => 71.1%
    [9] => 81.3%
    [10] => 91.5%
    [11] => 100%
    [12] => 100%
    [13] => 100%
[29] => Array
    [0] New File           1.6 m    PSTools.zip 13:37 -> 13:41
    [1] => 0.0%
[30] => 2015/06/25 13:45:17 ERROR 121 (0x00000079) Copying File c:testPSTools.zip
[31] => Array
    [0] => New File            1.6 m    PSTools.zip 13:45 -> 13:50
    [1] => 0.0%
    [2] => 15%
    [3] => 31%
    [4] => 46%
    [5] => 62%
    [6] => 77%
    [7] => 93%
    [8] => 99%
    [9] => 99%
    [10] => 100%

当然,如果有超过一个百分比,我想把它推到新数组的末尾,这样它就有了发展的历史。然后移到日志中的下一个文件。

我想知道是否有人对实现这一目标的最佳方式有什么想法?如果可能的话?

edit:我应该补充一点,我已经有一个函数,它会给我数组键,在它的值中包括%,它会返回布尔值yes或no。这是我无法理解的数据移动。

edit2:我意识到,如果没有文件名,子数组就没有什么意义。所以我修改了我想要的输出。

我想我已经找到了解决方案,它生成了一个可以使用的数组。为了至少填写那些可能正在寻找类似内容的内容,我使用了以下代码:

function strposa($haystack, $needles=array(), $offset=0){
    foreach($needles as $needle) {
        if (strpos($haystack, $needle) !== false) {
            return true;
        }
    }
    return false;
}

$modlog = array();
$addlog = array();
$x = 0;
reset($log);
while($x < count($log)){
    if(strposa(current($log), array("modified", "Newer", "New File")) !== false){
        $addlog["file"] = current($log);
        $x++;
        next($log);
        while(strposa(current($log), array("%", "ERROR")) !== false){
            $addlog[] = current($log);
            $x++;
            next($log);
        }
        $modlog[] = $addlog;
        unset($addlog); 
    }else{
        $modlog[] = current($log);
        $x++;
        next($log);
    }
}

这给了我样本输出:

[24] => Array
    (
        [file] => Newer                9.8 m    OKW3E04V101_tcm3-155664.exe
        [0] => 0.0%
        [1] => 2015/06/25 13:18:22 ERROR 53 (0x00000035) Copying File c:testOKW3E04V101_tcm3-155664.exe
    )
[25] => Array
    (
        [file] => Newer                9.8 m    OKW3E04V101_tcm3-155664.exe
        [0] => 2015/06/25 13:22:00 ERROR 53 (0x00000035) Copying File c:testOKW3E04V101_tcm3-155664.exe
    )
[26] => Array
    (
        [file] => Newer                9.8 m    OKW3E04V101_tcm3-155664.exe
        [0] => 0.0%
        [1] => 10.1%
        [2] => 20.3%
        [3] => 30.5%
        [4] => 40.6%
        [5] => 50.8%
        [6] => 61.0%
        [7] => 71.1%
        [8] => 81.3%
        [9] => 91.5%
        [10] => 100%
        [11] => 100%
        [12] => 100%
    )
[27] => Array
    (
        [file] => New File             1.6 m    PSTools.zip 13:37 -> 13:41
        [0] => 0%
        [1] => 2015/06/25 13:45:17 ERROR 121 (0x00000079) Copying File c:testPSTools.zip
    )
[28] => Array
    (
        [file] => New File             1.6 m    PSTools.zip 13:45 -> 13:50
        [0] => 0%
        [1] => 15%
        [2] => 31%
        [3] => 46%
        [4] => 62%
        [5] => 77%
        [6] => 93%
        [7] => 99%
        [8] => 99%
        [9] => 100%
    )

可能有更好的解决方案,但它似乎对我有效。如果有人有任何建议,请告诉我。

相关内容

  • 没有找到相关文章

最新更新