PHP - Filtering elements into a separate multidimensional array attached to previous key

0

I may be going about this in the complete wrong way, but I thought i'd first pose the question here. I have a log file generated from the Windows processes RoboCopy), which I have then opened up through PHP and created an array for every line of the log. I've then filtered through the array to only pull out the lines I need:

[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-lap\e$\test\
[2] => 2015/06/25 13:13:46 ERROR 53 (0x00000035) Accessing Destination Directory \\test-lap\e$\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:\test\OKW3E04V101_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:\test\OKW3E04V101_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:\test\PSTools.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%

What I'm attempting to do from here is whenever a percentage is shown, I want to unset the element and attach it to the previous element as a seperate array. So I would end up with something like this for each file reported:

[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-laptop\e$\test\
[2] => 2015/06/25 13:13:46 ERROR 53 (0x00000035) Accessing Destination Directory \\test-laptop\e$\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:\test\OKW3E04V101_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:\test\OKW3E04V101_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:\test\PSTools.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%

Of course if there is more than one percentage, I'd like to get it pushed onto the end on the new array, so it has a history of it's progression. This then moves onto the next file in the log.

I was wondering if anyone had any ideas on the best way to achieve this? If it's even possible?

edit: I should add that I already have a function that will give me the array keys that include % in it's value which will return a boolean yes or no. It's the moving of the data that I can't wrap my head around.

edit2: I realised that the child arrays would make little sense without the filename going with them. So I have amended my desired output.

php
arrays
multidimensional-array
asked on Stack Overflow Jun 29, 2015 by EJ Will • edited Jun 29, 2015 by EJ Will

1 Answer

0

I think I have found the solution, which generates an array that I can work with. To at least fill in those that maybe looking for something similar I used the code below:

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);
    }
}

This gave me the sample output of:

[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:\test\OKW3E04V101_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:\test\OKW3E04V101_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:\test\PSTools.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%
    )

There could be a nicer solution out there, but it seems to work for me. If anyone has any pointers then please let me know.

answered on Stack Overflow Jun 30, 2015 by EJ Will

User contributions licensed under CC BY-SA 3.0