Convert csv into array but split array by same date and execute a function on each array

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..


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