0

CSV:

0021044-1;16/02/2022;1;2;3;4;5;6;7;8;9;10;11;12;13;14;15
0021044-2;16/02/2022;1;2;3;4;5;6;7;8;9;10;11;12;13;14;15
0021064-1;21/01/2022;1;2;3;4;5;6;7;8;9;10;11;12;13;14;15
0021064-1;21/01/2022;1;2;3;4;5;6;7;8;9;10;11;12;13;14;15
0021067-1;19/01/2022;1;2;3;4;5;6;7;8;9;10;11;12;13;14;15
0021087-1;14/01/2022;1;2;3;4;5;6;7;8;9;10;11;12;13;14;15
0021087-2;14/01/2022;1;2;3;4;5;6;7;8;9;10;11;12;13;14;15

I'm splitting the array with the following PHP code:

$csv = array_map(function ($v) {
    return str_getcsv($v, ";");
}, file($file);
// Where file stands for the csv file being loaded.

Which gives me the following array

Array
(
    [0] => Array
        (
            [0] => 0021044-1
            [1] => 16/02/2022
            [2] => 1
            [3] => 2
            [4] => 3
            [5] => 4
            [6] => 5
            [7] => 6
            [8] => 7
            [9] => 8
            [10] => 9
            [11] => 10
            [12] => 11
            [13] => 12
            [14] => 13
            [15] => 14
            [16] => 15
        )

    [1] => Array
        (
            [0] => 0021044-2
            [1] => 16/02/2022
            [2] => 1
            [3] => 2
            [4] => 3
            [5] => 4
            [6] => 5
            [7] => 6
            [8] => 7
            [9] => 8
            [10] => 9
            [11] => 10
            [12] => 11
            [13] => 12
            [14] => 13
            [15] => 14
            [16] => 15
        )

    [2] => Array
        (
            [0] => 0021064-1
            [1] => 21/01/2022
            [2] => 1
            [3] => 2
            [4] => 3
            [5] => 4
            [6] => 5
            [7] => 6
            [8] => 7
            [9] => 8
            [10] => 9
            [11] => 10
            [12] => 11
            [13] => 12
            [14] => 13
            [15] => 14
            [16] => 15
        )

    [3] => Array
        (
            [0] => 0021064-1
            [1] => 21/01/2022
            [2] => 1
            [3] => 2
            [4] => 3
            [5] => 4
            [6] => 5
            [7] => 6
            [8] => 7
            [9] => 8
            [10] => 9
            [11] => 10
            [12] => 11
            [13] => 12
            [14] => 13
            [15] => 14
            [16] => 15
        )

    [4] => Array
        (
            [0] => 0021067-1
            [1] => 19/01/2022
            [2] => 1
            [3] => 2
            [4] => 3
            [5] => 4
            [6] => 5
            [7] => 6
            [8] => 7
            [9] => 8
            [10] => 9
            [11] => 10
            [12] => 11
            [13] => 12
            [14] => 13
            [15] => 14
            [16] => 15
        )

    [5] => Array
        (
            [0] => 0021087-1
            [1] => 14/01/2022
            [2] => 1
            [3] => 2
            [4] => 3
            [5] => 4
            [6] => 5
            [7] => 6
            [8] => 7
            [9] => 8
            [10] => 9
            [11] => 10
            [12] => 11
            [13] => 12
            [14] => 13
            [15] => 14
            [16] => 15
        )

    [6] => Array
        (
            [0] => 0021087-2
            [1] => 14/01/2022
            [2] => 1
            [3] => 2
            [4] => 3
            [5] => 4
            [6] => 5
            [7] => 6
            [8] => 7
            [9] => 8
            [10] => 9
            [11] => 10
            [12] => 11
            [13] => 12
            [14] => 13
            [15] => 14
            [16] => 15
        )

)

However, what I want to do is get the lines with the same date and perform a function on them

e.g;

Put the values

0021044-1;16/02/2022;1;2;3;4;5;6;7;8;9;10;11;12;13;14;15
0021044-2;16/02/2022;1;2;3;4;5;6;7;8;9;10;11;12;13;14;15
Then do a function on this array

0021064-1;21/01/2022;1;2;3;4;5;6;7;8;9;10;11;12;13;14;15
0021064-1;21/01/2022;1;2;3;4;5;6;7;8;9;10;11;12;13;14;15
Then do same function on this array
    
0021067-1;19/01/2022;1;2;3;4;5;6;7;8;9;10;11;12;13;14;15
Then do same function on this array

0021087-1;14/01/2022;1;2;3;4;5;6;7;8;9;10;11;12;13;14;15
0021087-2;14/01/2022;1;2;3;4;5;6;7;8;9;10;11;12;13;14;15
Then do same function on this array

So in short, the function should be applied to a newly generated array out of this one, array as follows:

$new_array = Array
(
    [0] => Array
        (
            [0] => 0021044-1
            [1] => 16/02/2022
            [2] => 1
            [3] => 2
            [4] => 3
            [5] => 4
            [6] => 5
            [7] => 6
            [8] => 7
            [9] => 8
            [10] => 9
            [11] => 10
            [12] => 11
            [13] => 12
            [14] => 13
            [15] => 14
            [16] => 15
        )

    [1] => Array
        (
            [0] => 0021044-2
            [1] => 16/02/2022
            [2] => 1
            [3] => 2
            [4] => 3
            [5] => 4
            [6] => 5
            [7] => 6
            [8] => 7
            [9] => 8
            [10] => 9
            [11] => 10
            [12] => 11
            [13] => 12
            [14] => 13
            [15] => 14
            [16] => 15
        )

do the function csv2xml($new_array); And then the second find of the other date etc... do the function again etc..

All the sub arrays created on their appropriate date will then go to a function which I have covered already in a function called csv2xml($arr)

Though, I'm not succeeding in splitting the array into array's per date.

Can someone guide me in the correct direction?

I think it's a lot of for, while and loops nested in eachother but my brain is currently melting on this..

4
  • 2
    How big is your CSV file? Depending on that, if it's not so big, you could build a dictionnary (array with string as keys) with the second field (the date) as key. If the entry doesn't exist yet then create an array for that entry. Then fill it with the other fields of your CSV line. This way you'll get them grouped and you'll then be able to iterate over all of them to do your work on them. Commented Jan 11, 2022 at 14:28
  • PatrickJanser The csv has a maximum of 100 lines, so I don't think it's too big for that handling @RiggsFolly: I've editted the question trying to be as understandable as possible Commented Jan 11, 2022 at 14:30
  • 1
    Great, yes, even if it was 1000 lines it wouldn't take much memory. Commented Jan 11, 2022 at 14:31
  • 2
    By the way, if the dates are ordered (it looks like a log file so it should be) then you could just use a memory variable and check if the current date is different or not of the last date you memorised during the previous iteration. This way you wouldn't even need to do a second loop to run your grouped operation. Commented Jan 11, 2022 at 14:35

1 Answer 1

1

If the rows will always have the 2 dates following each other you can simply walk through the file calling your function on every other line, with a little check for those dates where only one line exists

I made up a little function to mimic your call that just prints the 2 dates to make sure it works.

function call_function($a1, $a2)
{
    echo sprintf( "The 2 dates are %s and %s\n", $a1[1] , $a2[1]);
}


$f = fopen('tst.csv', 'r');

$last_date = NULL;
$last_line = NULL;
while ( ($line = fgetcsv($f, 1024, ';')) !== FALSE){
    if ( $line[1] == $last_date ){
        // we got the second of a set of dates so call your function
        call_function($last_line, $line);
    } else {
        $last_date = $line[1];
        $last_line = $line;
    }
}

RESULTS

The 2 dates are 16/02/2022 and 16/02/2022
The 2 dates are 21/01/2022 and 21/01/2022
The 2 dates are 14/01/2022 and 14/01/2022
Sign up to request clarification or add additional context in comments.

1 Comment

Awesome @RiggsFolly, you guided me in the way I was looking for, altered the code a bit due to more than 2 results from time to time. Thank you for your guidance

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.