doulin1867 2013-06-07 04:24
浏览 32
已采纳

PHP - 当filename不是in_array时fputcsv

I have previously iterated thru a filesystem and uploaded a large number of files whose filename's matched a string and all the matched strings are in an array.

I am now trying to iterate thru that same file system to output a csv file of all the file's that did NOT get uploaded on the first pass.

// The SPL object is working fine here; problem is in my logic below ...
$it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path);

foreach($it as $file){
    $filename = $file->getFilename();
    $uploaded_filenames = ['face', 'arm', 'leg'];

    foreach($uploaded_filenames as $up){
        if(strpos($filename, $up) !== false {
            // do nothing!
        }else{
            fputcsv($handle, $filename);
        }
    }
}

Just looking for a more /correct/elegant way of doing this

Thank you Orangepill, that just about worked. The exact syntax:

$in_array = false;
foreach($this->uploaded_filenames as $whatever){

    if(strpos($filename, $whatever) !== false){
        $in_array = true;
        break;
    }

}

if(!$in_array) fputcsv($handle, $fullpath);

Thanks again!

  • 写回答

1条回答 默认 最新

  • dpmrakfbx820320638 2013-06-07 04:28
    关注

    I think what you are looking for is

    $inArray = false;
    foreach($uploaded_filenames as $up){
        if(strpos($filename, $up) !== false) {
            $inArray = true;
            break;
        }
    }
    if (!$inArray)){
        fputcsv ($handle, $filename);    
    }
    

    instead of

    foreach($uploaded_filenames as $up){
            if(strpos($filename, $up) !== false) {
                // do nothing!
            }else{
                fputcsv($handle, $filename);
            }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有偿求码,CNN+LSTM实现单通道脑电信号EEG的睡眠分期评估
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路