Link to home
Start Free TrialLog in
Avatar of hydrazi
hydrazi

asked on

Skipping lines of a CSV file import in PHP

I am importing a set of orders from a CSV file.  I want to remove certain rows from the rest of the script based on the value of a certain field.  

Would I first load the entire CSV file into an array?  How would I test and remove the rows I do not want to show up?
SOLUTION
Avatar of Dénes Kellner
Dénes Kellner
Flag of Hungary image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
If your file is REALLY big, you can do it with 2 files open: one for reading, another for writing, and instead of filling a variable like "$output", write to file2 so that you only need memory space for a single line, not the whole output.
Avatar of hydrazi
hydrazi

ASKER

Alright, let me see if I can figure that out.  I'm a bit of a newbie!  Many thanks for this!
Avatar of hydrazi

ASKER

The file is pretty small.  Maybe a max of 40 rows.  
If you give me some details (like the file itself) I can do it for you.  But try by yourself coz that's how newbies become pros!
Avatar of hydrazi

ASKER

I am unsure what logic to use for the function.  If I could get a hint...  
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
oh almost forgot, this is a plain text file for the  file  test.csv, it is One Line of text as =
0 ,1 ,2 ,3 ,4 ,5 ,6 ,7 ,8 ,9 ,10 ,11 ,12 ,13 ,14 ,15 ,16 ,17 ,18 ,19
Avatar of hydrazi

ASKER

Alrighty..... working on this again!  Sorry for the delay!
Avatar of hydrazi

ASKER

I DID IT!  Many thanks for all the pointers!
Avatar of hydrazi

ASKER

ooops..... ok, I tried Slick812's code, just on it's own to understand it better.  Then I tried it with our CSV file that is delimited with a "|" which I changed in the code.  However, it did not remove the offending rows.  Not even the offending field.  Here is what I changed it to, but it still prints the row I want removed:


<?php
$doNotUse = array("ppfc","plot plan");
$aryCSV = file_get_contents("orders.csv");
$aryCSV = explode('|', $aryCSV);//makes string into an array
$arySize=count($aryCSV);
for ($i = 0; $i < $arySize; ++$i){
    // use in_array() to see if the value is in the $doNotUse array
    if (in_array($i, $doNotUse)) unset($aryCSV[$i]);
    // unset() will remove the array element
    }
echo 'Comma Separated Values after Removal= ';
foreach ($aryCSV as $val) echo $val,'|';
echo '<br />';

?>

Open in new window

OK, you do not seem to understand the workings of the code I supplied, because I did not know how your CSV was used as text, I used "Numbers" as 0, 1, 2, 3 as the
$doNotUse = array(2,3,6,7,12);
array,
but for you you have changed it to -
$doNotUse = array("ppfc","plot plan");

which will not work for this or any other CSV file that you use -
$aryCSV = explode('|', $aryCSV);
to get an array. The $aryCSV array will always have Numbered elements as -
$aryCSV[0] , $aryCSV[1] , $aryCSV[2],
and never any as -
$aryCSV["ppfc"], $aryCSV["plot plan"]

as I said before "However, the way you might test for the removal "number" as an array element would greatly depend on what you had as the data container for the excluded array element number."

this is the line of code that "TESTS" for a correct or incorrect element key or value in the $aryCSV -
if (in_array($i, $doNotUse)) unset($aryCSV[$i]);

you will need to change this to your own way of testing for what you want to leave in or take out of the $aryCSV array,

I suspect that the  "ppfc" and "plot plan" are values in the array and not keys, is this correct ?

OK, I did something that will test for Values, instead of Keys in the CSV array, I use the PHP function array_search( ) which you can see what it does here -
http://www.php.net/manual/en/function.array-search.php

I changed the FOR loop to go through the shorter (I guess, but it may not be true depending on the way you fill the $doNotUse) $doNotUse array


The 'test.csv' file is ONE line of text as-
first 0|second 1|third 2|fourth 3|fifth 4|sixth 5|seven 6|last 7




I hope this will help, but your test to unset( ) will depend on what is in the $doNotUse  array
$doNotUse = array('second 1','bogus 99','fifth 5');
$aryCSV = file_get_contents('test.csv');
$aryCSV = explode('|', $aryCSV);
//$arySize=count($aryCSV); // old way
$arySize=count($doNotUse);// LOOK this has changed
for ($i = 0; $i < $arySize; ++$i){
	//if (in_array($i, $doNotUse)) unset($aryCSV[$i]);// old way
	$key = array_search($doNotUse[$i], $aryCSV);// LOOK this is different
	if($key) unset($aryCSV[$key]);
	}
echo 'Comma Separated Values after Removal= ';
foreach ($aryCSV as $val) echo $val,',';
echo '<br />';

Open in new window