How would I modify this fgetcsv to check multiple columns (not just one!)
How would I modify this fgetcsv to check multiple columns (not just one!)
I have a working script below which checks a column (1) for something and if found, looks at the adjacent column (0) and does some stuff. (i've excluded stuff above the important code but it loops round every file in a directory as $thisGame or thisGameNoExtension which is the same just with no file extension!
$thisGame
thisGameNoExtension
$csvFile = fopen("ManualRegionDupes.csv", "r");
while ($csvRows = fgetcsv($csvFile))
{
if ($csvRows[1] == $thisGameNoExtension)
{
$primaryGame = $csvRows[0];
$fileExtension = pathinfo($thisGame, PATHINFO_EXTENSION);
$fileCheck = trim(shell_exec("ls -1 " . escapeshellarg($primaryGame) . "." . escapeshellarg($fileExtension) . " 2>/dev/null"));
if ($fileCheck)
{
echo "33[00;33m is on the Manual Region Dupes list and primary version detected. Moved to Removed foldern";
shell_exec("mv " . escapeshellarg($thisGame) . " Removed/");
continue 2;
}
else
{
echo "33[00;33m is on the Manual Region Dupes list but primary version is missing. Kept.n";
continue 2;
}
}
}
fclose($csvFile);
I need to create a way of looking at 3 columns (1,2 & 3) not just one. If any of these columns contain what i'm looking for then I want to continue the script. I'm not sure how to acheive this. I've tried:
if ($csvRows[1,2,3] == $thisGameNoExtension)
if ($csvRows[1,2,3] == $thisGameNoExtension)
and also
`if ($csvRows[1] == $thisGameNoExtension) || if ($csvRows[2] == $thisGameNoExtension) || if ($csvRows[3] == $thisGameNoExtension)
But both are incorrect syntax - hopefully they show what i'm trying to acheive though! I'm sure there is a fairly simple solution, apologies for being a dimwit and thankyou in advance for any help you can give me!
Many thanks.
1 Answer
1
This is pretty basic stuff, but:
if ($csvRows[1] == $thisGameNoExtension || $csvRows[2] == $thisGameNoExtension || $csvRows[3] == $thisGameNoExtension) {
You don't specify the if keyword for each predicate.
if
Edit: In response to comment, change your if statement to:
if
if (in_array($thisGameNoExtension, array_slice($csvRows, 0, 3))) {
Hi Sean. Is there a way of stopping the "Undefined Offset" notices I'm getting? These are due to the fact cells in Column 2 and 3 are sometimes empty. Many thanks!
– UnluckyForSome9
4 mins ago
@UnluckyForSome9, see my edit
– Sean Bright
52 secs ago
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Thanks. I did try it this way too but I was getting a syntax error, but I'm thinking now I probably just missed the bracket on the end! Doh!
– UnluckyForSome9
30 mins ago