Reputation: 381
I am trying to pass if()
statement with !in_array()
check for the generated csv
file.
End result is returning false for some reason, and I can not find what am I doing wrong. I am trying to pass false statement and continue with code execution.
Input:
Array with data I want to put into csv file.
Creating and inputing data into file.
Getting the file
$list = [
0 => [
'materials'
],
1 => [
'products'
]
];
$testCsvFile = fopen("test.csv", "w");
foreach ($list as $fields) {
fputcsv($testCsvFile, $fields);
}
$projectRoot = substr(strrchr(getcwd(), DIRECTORY_SEPARATOR), 1);
$getFile = $projectRoot . '/test.csv';
$file = basename($getFile);
if ($list && is_array($list)) {
unset($parsed_data[0]);
}
if (!in_array($file, ['materials', 'products'])) {
return false;
}
.... next //
Upvotes: 0
Views: 97
Reputation: 160
you're searching the strings without reading the file you should first open the file , read it then you should find string in array, I've pasted below code just for your reference which is searching the string in csv file after opening and reading.
$file = fopen("test.csv", "r");
$str = "materials";
while (($data = fgetcsv($file)) !== false)
{
if(in_array($str, $data))
{
foreach ($data as $i)
{
echo $i."<br>";
}
}
}
fclose($file);
Upvotes: 1