$file = file('file.csv');
$counter=0;
foreach($file as $k){
if(preg_match('/"/', $k)==1){
$csv[] = explode(',', $k);
}
foreach($k as $key => $value){
if($value == 'specific value'){
$counter++;
}
}
}
// print_r($csv);
echo $counter;
$k is outputting correctly as an array of comma-separated values; I'm trying to analyze the comma-separated values of each line $k ... getting "Invalid argument supplied for foreach()" error ...
Check, wether $k
is an array (is_array($k)
and if it is not empty !empty($k)
(same as count(arr)>0). Also you should check your CSV-file for errors like blank lines, hidden returns/tabs/etc. .
$file = file('file.csv');
$counter=0;
foreach($file as $k){
if(preg_match('/"/', $k)==1){
$csv[] = explode(',', $k);
}
if (!is_array($k) OR empty($k)) { continue; }
foreach($k as $key => $value){
if($value == 'specific value'){
$counter++;
}
}
}
// print_r($csv);
echo $counter;
Seems to me $k is a string, while $csv is the array you wanna iterate over.