我将如何修改此 fgetcsv 以检查多列(不仅仅是一列!)
How would I modify this fgetcsv to check multiple columns (not just one!)
我在下面有一个工作脚本,它检查列 (1) 中的内容,如果找到,则查看相邻的列 (0) 并执行一些操作。 (我已经排除了重要代码之上的内容,但它会以 $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 "3[00;33m is on the Manual Region Dupes list and primary version detected. Moved to Removed folder\n";
shell_exec("mv " . escapeshellarg($thisGame) . " Removed/");
continue 2;
}
else
{
echo "3[00;33m is on the Manual Region Dupes list but primary version is missing. Kept.\n";
continue 2;
}
}
}
fclose($csvFile);
我需要创建一种查看 3 列(1,2 和 3)的方法,而不仅仅是一列。如果这些列中的任何一个包含我正在寻找的内容,那么我想继续执行该脚本。我不确定如何实现这一目标。我试过:
if ($csvRows[1,2,3] == $thisGameNoExtension)
还有
`if ($csvRows[1] == $thisGameNoExtension) || if ($csvRows[2] == $thisGameNoExtension) || if ($csvRows[3] == $thisGameNoExtension)
但两者都是不正确的语法 - 希望它们能展示我正在努力实现的目标!我确定有一个相当简单的解决方案,很抱歉我是个笨蛋,并提前感谢您能给我的任何帮助!
非常感谢。
这是非常基本的东西,但是:
if ($csvRows[1] == $thisGameNoExtension || $csvRows[2] == $thisGameNoExtension || $csvRows[3] == $thisGameNoExtension) {
您没有为每个谓词指定 if
关键字。
编辑: 为回应评论,将您的 if
语句更改为:
if (in_array($thisGameNoExtension, array_slice($csvRows, 0, 3))) {
我在下面有一个工作脚本,它检查列 (1) 中的内容,如果找到,则查看相邻的列 (0) 并执行一些操作。 (我已经排除了重要代码之上的内容,但它会以 $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 "3[00;33m is on the Manual Region Dupes list and primary version detected. Moved to Removed folder\n";
shell_exec("mv " . escapeshellarg($thisGame) . " Removed/");
continue 2;
}
else
{
echo "3[00;33m is on the Manual Region Dupes list but primary version is missing. Kept.\n";
continue 2;
}
}
}
fclose($csvFile);
我需要创建一种查看 3 列(1,2 和 3)的方法,而不仅仅是一列。如果这些列中的任何一个包含我正在寻找的内容,那么我想继续执行该脚本。我不确定如何实现这一目标。我试过:
if ($csvRows[1,2,3] == $thisGameNoExtension)
还有
`if ($csvRows[1] == $thisGameNoExtension) || if ($csvRows[2] == $thisGameNoExtension) || if ($csvRows[3] == $thisGameNoExtension)
但两者都是不正确的语法 - 希望它们能展示我正在努力实现的目标!我确定有一个相当简单的解决方案,很抱歉我是个笨蛋,并提前感谢您能给我的任何帮助!
非常感谢。
这是非常基本的东西,但是:
if ($csvRows[1] == $thisGameNoExtension || $csvRows[2] == $thisGameNoExtension || $csvRows[3] == $thisGameNoExtension) {
您没有为每个谓词指定 if
关键字。
编辑: 为回应评论,将您的 if
语句更改为:
if (in_array($thisGameNoExtension, array_slice($csvRows, 0, 3))) {