-1

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] => 
        )
    )

)  
6
  • Have you already done anything to achieve this? Can you show your code? What should the new TRANS array contain? Commented Jul 10, 2018 at 10:06
  • Please modify your question and make clear your problem Commented Jul 10, 2018 at 10:06
  • Do you want to separate TRANS data from the current data? Commented Jul 10, 2018 at 10:07
  • Can you show us the desired output please? Commented Jul 10, 2018 at 10:10
  • So i want to split the array each time TRANS appears most the time this is every 9th array item but on the odd occasion it can be the 10th or 11th array item Commented Jul 10, 2018 at 10:11

2 Answers 2

0

It is very simple:

$result = array();
foreach ($data as $row) {
    if ($row[0] === "TRANS") {
        $result[] = array();
    }
    $result[count($result) - 1][] = $row;
}
Sign up to request clarification or add additional context in comments.

Comments

0

Please try this code

 $data =  csv_to_array('test.csv');
$transArr = array();
$restArr = array();
foreach($data as $singleArr){   
    if(in_array("TRANS",$singleArr)){
     $transArr[] =  $singleArr;
    }else{
        $restArr[] =  $singleArr;
    }
}
$reslArr = array();
foreach($transArr as $singleA){   
    $temp = array();
     $temp[] = $singleA;
     $reslArr[] = array_merge($temp,$restArr);

}
echo '<pre>';print_r($reslArr);echo '</pre>';

6 Comments

That only pulls all the TRANS items i still need the other data aswell
@Rusty1182, I think he is looking like this. Please can elaborate your problem.
I have updated the abouve array example to show what im trying to do you can see every TRANS is the start of a new array
i have tried using array chunk for evey 9 items but TRANS is not always the 9th item
@Rusty1182, wait for i am trying
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.