phparrayssplit

PHP break array each time the a specified value appears into separate arrays


I have a PHP array that has been gathered from a CSV file

I have then removed the Header and I'm now left with this array and wish to split/ break an array for each occurrence of TRANS so for every [0] => TRANS we have a new array.

I have tried using array chunk

  foreach (array_chunk($data, 9, true) as $row) {
      $data2[] = $row;
  }

but the TRANS line is not always the 9th item

any help would be much appreciated

Thanks

Ant

this is my array

Array
(
[0] => Array
    (
        [0] => TRANS
        [1] => 138
        [2] => 
        [3] => 10008710
        [4] => APPNT
        [5] => COS
        [6] => 
        [7] => 
        [8] => 
        [9] => 
        [10] => D
        [11] => 
        [12] => 
        [13] => 
        [14] => 20180516
    )

[1] => Array
    (
        [0] => MTPNT
        [1] => 
        [2] => 2477540807
        [3] => F
        [4] => 
        [5] => 
        [6] => 
        [7] => 
        [8] => 
        [9] => 20170925
        [10] => 
        [11] => 
        [12] => 
        [13] => 
        [14] => 
    )

[2] => Array
    (
        [0] => ADDRS
        [1] => MTRPT
        [2] => 
        [3] => 
        [4] => 1
        [5] => 
        [6] => DATA
        [7] => 
        [8] => 
        [9] => DATA
        [10] => DATA
        [11] => DATA
        [12] => 
        [13] => 
        [14] => 
    )

[3] => Array
    (
        [0] => ASSET
        [1] => 
        [2] => APPNT
        [3] => METER
        [4] => 
        [5] => 
        [6] => E6VG470
        [7] => LPG
        [8] => 2017
        [9] => E6S01910141760
        [10] => 0
        [11] => 
        [12] => LI
        [13] => 
        [14] => 
    )

[4] => Array
    (
        [0] => METER
        [1] => 
        [2] => U
        [3] => S1
        [4] => 
        [5] => 
        [6] => 
        [7] => 
        [8] => S
        [9] => 
        [10] => 
        [11] => 
        [12] => 
        [13] => 
        [14] => 
    )

[5] => Array
    (
        [0] => REGST
        [1] => 
        [2] => METER
        [3] => 5
        [4] => SCMH
        [5] => 
        [6] => 
        [7] => 
        [8] => 
        [9] => 
        [10] => 
        [11] => 
        [12] => 
        [13] => 
        [14] => 
    )

[6] => Array
    (
        [0] => MKPRT
        [1] => MAM
        [2] => MPS
        [3] => 
        [4] => 
        [5] => 
        [6] => 
        [7] => 
        [8] => 
        [9] => 
        [10] => 
        [11] => 
        [12] => 
        [13] => 
        [14] => 
    )

[7] => Array
    (
        [0] => NAME
        [1] => CONT
        [2] => Mrs
        [3] => M
        [4] => CARTER
        [5] => 
        [6] => 
        [7] => 
        [8] => 
        [9] => 
        [10] => 
        [11] => 
        [12] => 
        [13] => 
        [14] => 
    )

[8] => Array
    (
        [0] => CONTM
        [1] => TEL
        [2] => 345345345
        [3] => 
        [4] => 
        [5] => 
        [6] => 
        [7] => 
        [8] => 
        [9] => 
        [10] => 
        [11] => 
        [12] => 
        [13] => 
        [14] => 
    )

[9] => Array
    (
        [0] => TRANS
        [1] => 139
        [2] => 
        [3] => 10008710
        [4] => APPNT
        [5] => COS
        [6] => 
        [7] => 
        [8] => 
        [9] => 
        [10] => D
        [11] => 
        [12] => 
        [13] => 
        [14] => 20180516
    )

The above array continues in the same pattern starting with TRANS

This is the code used so far

function csv_to_array($filename='', $delimiter=',')
{
if(!file_exists($filename) || !is_readable($filename))
    return FALSE;

$data = array();
if (($handle = fopen($filename, 'r')) !== FALSE)
{
    while (($row = fgetcsv($handle, 1000, $delimiter)) !== FALSE)
    {

            $data[] = $row;
    }
    fclose($handle);
}
return $data;
}

$data =  csv_to_array('test.csv');

echo'EXTRACTED HEADER';

$header = $data['0'];
echo '<pre>';
print_r($header);
echo '</pre>';

echo'DATA';

array_shift($data);
array_pop($data);

echo '<pre>';
print_r($data);
echo '</pre>';

The output would be like so...

Array
(
[0] => Array
    (
    [0] => Array
        (
            [0] => TRANS
            [1] => 138
            [2] => 
            [3] => 10008710
            [4] => APPNT
            [5] => COS
            [6] => 
            [7] => 
            [8] => 
            [9] => 
            [10] => D
            [11] => 
            [12] => 
            [13] => 
            [14] => 20180516
        )

    [1] => Array
        (
            [0] => MTPNT
            [1] => 
            [2] => 2477540807
            [3] => F
            [4] => 
            [5] => 
            [6] => 
            [7] => 
            [8] => 
            [9] => 20170925
            [10] => 
            [11] => 
            [12] => 
            [13] => 
            [14] => 
        )

    [2] => Array
        (
            [0] => ADDRS
            [1] => MTRPT
            [2] => 
            [3] => 
            [4] => 1
            [5] => 
            [6] => DATA
            [7] => 
            [8] => 
            [9] => DATA
            [10] => DATA
            [11] => DATA
            [12] => 
            [13] => 
            [14] => 
        )

    [3] => Array
        (
            [0] => ASSET
            [1] => 
            [2] => APPNT
            [3] => METER
            [4] => 
            [5] => 
            [6] => E6VG470
            [7] => LPG
            [8] => 2017
            [9] => E6S01910141760
            [10] => 0
            [11] => 
            [12] => LI
            [13] => 
            [14] => 
        )

    [4] => Array
        (
            [0] => METER
            [1] => 
            [2] => U
            [3] => S1
            [4] => 
            [5] => 
            [6] => 
            [7] => 
            [8] => S
            [9] => 
            [10] => 
            [11] => 
            [12] => 
            [13] => 
            [14] => 
        )

    [5] => Array
        (
            [0] => REGST
            [1] => 
            [2] => METER
            [3] => 5
            [4] => SCMH
            [5] => 
            [6] => 
            [7] => 
            [8] => 
            [9] => 
            [10] => 
            [11] => 
            [12] => 
            [13] => 
            [14] => 
        )

    [6] => Array
        (
            [0] => MKPRT
            [1] => MAM
            [2] => MPS
            [3] => 
            [4] => 
            [5] => 
            [6] => 
            [7] => 
            [8] => 
            [9] => 
            [10] => 
            [11] => 
            [12] => 
            [13] => 
            [14] => 
        )

    [7] => Array
        (
            [0] => NAME
            [1] => CONT
            [2] => Mrs
            [3] => M
            [4] => CARTER
            [5] => 
            [6] => 
            [7] => 
            [8] => 
            [9] => 
            [10] => 
            [11] => 
            [12] => 
            [13] => 
            [14] => 
        )

    [8] => Array
        (
            [0] => CONTM
            [1] => TEL
            [2] => 345345345
            [3] => 
            [4] => 
            [5] => 
            [6] => 
            [7] => 
            [8] => 
            [9] => 
            [10] => 
            [11] => 
            [12] => 
            [13] => 
            [14] => 
        )
    )
[1] => Array 
    (    

    [0] => Array
        (
            [0] => TRANS
            [1] => 138
            [2] => 
            [3] => 10008710
            [4] => APPNT
            [5] => COS
            [6] => 
            [7] => 
            [8] => 
            [9] => 
            [10] => D
            [11] => 
            [12] => 
            [13] => 
            [14] => 20180516
        )

    [1] => Array
        (
            [0] => MTPNT
            [1] => 
            [2] => 2477540807
            [3] => F
            [4] => 
            [5] => 
            [6] => 
            [7] => 
            [8] => 
            [9] => 20170925
            [10] => 
            [11] => 
            [12] => 
            [13] => 
            [14] => 
        )

    [2] => Array
        (
            [0] => ADDRS
            [1] => MTRPT
            [2] => 
            [3] => 
            [4] => 1
            [5] => 
            [6] => DATA
            [7] => 
            [8] => 
            [9] => DATA
            [10] => DATA
            [11] => DATA
            [12] => 
            [13] => 
            [14] => 
        )

    [3] => Array
        (
            [0] => ASSET
            [1] => 
            [2] => APPNT
            [3] => METER
            [4] => 
            [5] => 
            [6] => E6VG470
            [7] => LPG
            [8] => 2017
            [9] => E6S01910141760
            [10] => 0
            [11] => 
            [12] => LI
            [13] => 
            [14] => 
        )

    [4] => Array
        (
            [0] => METER
            [1] => 
            [2] => U
            [3] => S1
            [4] => 
            [5] => 
            [6] => 
            [7] => 
            [8] => S
            [9] => 
            [10] => 
            [11] => 
            [12] => 
            [13] => 
            [14] => 
        )

    [5] => Array
        (
            [0] => REGST
            [1] => 
            [2] => METER
            [3] => 5
            [4] => SCMH
            [5] => 
            [6] => 
            [7] => 
            [8] => 
            [9] => 
            [10] => 
            [11] => 
            [12] => 
            [13] => 
            [14] => 
        )

    [6] => Array
        (
            [0] => MKPRT
            [1] => MAM
            [2] => MPS
            [3] => 
            [4] => 
            [5] => 
            [6] => 
            [7] => 
            [8] => 
            [9] => 
            [10] => 
            [11] => 
            [12] => 
            [13] => 
            [14] => 
        )

    [7] => Array
        (
            [0] => NAME
            [1] => CONT
            [2] => Mrs
            [3] => M
            [4] => CARTER
            [5] => 
            [6] => 
            [7] => 
            [8] => 
            [9] => 
            [10] => 
            [11] => 
            [12] => 
            [13] => 
            [14] => 
        )

    [8] => Array
        (
            [0] => CONTM
            [1] => TEL
            [2] => 345345345
            [3] => 
            [4] => 
            [5] => 
            [6] => 
            [7] => 
            [8] => 
            [9] => 
            [10] => 
            [11] => 
            [12] => 
            [13] => 
            [14] => 
        )
    )
[2] => Array
    (
    [0] => Array
        (
            [0] => TRANS
            [1] => 138
            [2] => 
            [3] => 10008710
            [4] => APPNT
            [5] => COS
            [6] => 
            [7] => 
            [8] => 
            [9] => 
            [10] => D
            [11] => 
            [12] => 
            [13] => 
            [14] => 20180516
        )

    [1] => Array
        (
            [0] => MTPNT
            [1] => 
            [2] => 2477540807
            [3] => F
            [4] => 
            [5] => 
            [6] => 
            [7] => 
            [8] => 
            [9] => 20170925
            [10] => 
            [11] => 
            [12] => 
            [13] => 
            [14] => 
        )

    [2] => Array
        (
            [0] => ADDRS
            [1] => MTRPT
            [2] => 
            [3] => 
            [4] => 1
            [5] => 
            [6] => DATA
            [7] => 
            [8] => 
            [9] => DATA
            [10] => DATA
            [11] => DATA
            [12] => 
            [13] => 
            [14] => 
        )

    [3] => Array
        (
            [0] => ASSET
            [1] => 
            [2] => APPNT
            [3] => METER
            [4] => 
            [5] => 
            [6] => E6VG470
            [7] => LPG
            [8] => 2017
            [9] => E6S01910141760
            [10] => 0
            [11] => 
            [12] => LI
            [13] => 
            [14] => 
        )

    [4] => Array
        (
            [0] => METER
            [1] => 
            [2] => U
            [3] => S1
            [4] => 
            [5] => 
            [6] => 
            [7] => 
            [8] => S
            [9] => 
            [10] => 
            [11] => 
            [12] => 
            [13] => 
            [14] => 
        )

    [5] => Array
        (
            [0] => REGST
            [1] => 
            [2] => METER
            [3] => 5
            [4] => SCMH
            [5] => 
            [6] => 
            [7] => 
            [8] => 
            [9] => 
            [10] => 
            [11] => 
            [12] => 
            [13] => 
            [14] => 
        )

    [6] => Array
        (
            [0] => MKPRT
            [1] => MAM
            [2] => MPS
            [3] => 
            [4] => 
            [5] => 
            [6] => 
            [7] => 
            [8] => 
            [9] => 
            [10] => 
            [11] => 
            [12] => 
            [13] => 
            [14] => 
        )

    [7] => Array
        (
            [0] => NAME
            [1] => CONT
            [2] => Mrs
            [3] => M
            [4] => CARTER
            [5] => 
            [6] => 
            [7] => 
            [8] => 
            [9] => 
            [10] => 
            [11] => 
            [12] => 
            [13] => 
            [14] => 
        )

    [8] => Array
        (
            [0] => CONTM
            [1] => TEL
            [2] => 345345345
            [3] => 
            [4] => 
            [5] => 
            [6] => 
            [7] => 
            [8] => 
            [9] => 
            [10] => 
            [11] => 
            [12] => 
            [13] => 
            [14] => 
        )
    )

)  

Solution

  • It is very simple:

    $result = array();
    foreach ($data as $row) {
        if ($row[0] === "TRANS") {
            $result[] = array();
        }
        $result[count($result) - 1][] = $row;
    }