This question already has an answer here:
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,
</div>
To remove an element from an array,
unset($dataArray[0]);
Note that the other element indexes are the same.
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"
}