0

I was wondering if someone could help me out.

I have a function that imports a CSV into an array, but i need to remove the first line with the

My CSV is formatted like so:

lat,lng,id
-34.837834,1387457,2

and so on, you get the ID

My function looks like this

private function getArrayFromCsv($file,$delimiter)
{ 

    if (($handle = fopen($file, "r")) !== FALSE) { 

        $i = 0; 
        while (($lineArray = fgetcsv($handle, 4000, $delimiter)) !== FALSE) { 

            for ($j=0; $j<count($lineArray); $j++) { 

                $dataArray[$i][$j] = $lineArray[$j]; 

            } 

            $i++; 

        } 

        fclose($handle); 

    } 

    return $dataArray; 

}

The array that is outputted is as follows:

Array
(
    [0] => Array
        (
            [0] => lat
            [1] => lng
            [2] => id
        )

    [1] => Array
        (
            [0] => -34.837834
            [1] => 1387457
            [2] => 2

and so on .....

How do i go about removing the array that shows the lat,lng,id and start from the next line in the CSV?

Cheers,

r5d
  • 579
  • 5
  • 24
BigJobbies
  • 499
  • 8
  • 20
  • 1
    You use unset. This question has been answered in detail at: http://stackoverflow.com/questions/369602/delete-an-element-from-an-array – tlukechess Oct 26 '14 at 01:57

2 Answers2

4

To remove an element from an array,
unset($dataArray[0]);
Note that the other element indexes are the same.

Tom Hoang
  • 314
  • 1
  • 5
2

Alternatively, you could use array_shift, this will reset the indexes on the array as well, while unset does not.

$unsetTest = array('hey','now','brown','cow');
$shiftTest = array('hey','now','brown','cow');

unset($unsetTest[0]);
array_shift($shiftTest);

var_dump($unsetTest);
var_dump($shiftTest);

//output

array(3) {
  [1]=>
  string(3) "now"
  [2]=>
  string(5) "brown"
  [3]=>
  string(3) "cow"
}

array(3) {
  [0]=>
  string(3) "now"
  [1]=>
  string(5) "brown"
  [2]=>
  string(3) "cow"
}
bruchowski
  • 5,043
  • 7
  • 30
  • 46