douzhuanfen5923 2017-02-23 20:50
浏览 28
已采纳

迭代一个CSV文件并跳过一圈?

I want to iterate over a CSV file and check if on position row[1] are certain countries. If yes, the while lap should be skipped to the next row.

But at the moment it is failing due to my if condition:

$countries = array("Österreich", "Schweiz", "Niederlande", "Großbritannien", "United States");

while (($row = fgetcsv($inputFile, 0, $delimiter)) !== false) {
        foreach ($countries as $country){
            if(((stripos($row[1], $country === false))) && stripos($row[0], "Card")){
                echo $row[0] . "\t => \t" . $row[1] . "
";
            }
        }
... some other code (Dealing with the values) ...     
}

To continue coding I was checking if my statement is right and if I get the desired values. But I do not.

I want only to get the values where the country is not one of the countries in the array and if the title in row[1] contains the word card. But if I use this if condition I get no values. But what is wrong?

This is a CSV sample:

products_name;products_edition
"PlayStation Network Card 20€";"Deutschland ";
"PlayStation Network Card 50€";Österreich;
"Playstation Plus Live Card - 365 Tage";Deutschland;
"PlayStation Network Card 10£ ";Großbritannien;
"PlayStation Network Card 20$";"United States";

So actual I should get "PlayStation Network Card 20€";"Deutschland "; and "Playstation Plus Live Card - 365 Tage";Deutschland;. But I do not...

Does anybody know why and if yes how can I fix that?

And my second question is: If one value of products_edition is matching with one country from my array, the actual while lap should get skipped because I do not want to handle the product if it is from those countries. But how can I tell that that actual while lap?

I do not want to stop the hole while loop, only the while lap where row[1] is matching one of the countries in my country array.

Does anybody has an idea? Greetings and Thank You!

EDIT: I know now how to skip the actual loop lap and to tell the loop to continue with the next lap. I just use continue;. But there is still my other if condition problem... :/

  • 写回答

1条回答 默认 最新

  • douya5194 2017-02-23 21:22
    关注
    $countries = array("Österreich", "Schweiz", "Niederlande", "Großbritannien", "United States");
    while (($row = fgetcsv($inputFile, 0, $delimiter)) !== false) {
        if (in_array($row[1], $countries) && strpos($row['1'], "Card") !== false) {
            continue;
        }
        echo $row[0] . "\t => \t" . $row[1] . "
    ";
    //... some other code (Dealing with the values) ...     
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果