dsa45664 2010-10-02 12:08
浏览 29
已采纳

帮助嵌套的foreach循环

I am trying to ultimately find files in a directory that are no longer being used and delete them. I am using a field in my database to match against the filenames; they are the same.

The outer loop is for the database values and the inner loop is for the files in the directory. I'm using an if stmt inside the inner loop but I am getting files showing up in the output that should not be there. What am I doing wrong?

If I have a file in my directory and no matching database value, I want to delete the file.

                foreach( $ds as $id ){
                    foreach( $out as $file ){
                        if( $file != $id['patient_id'] ){
                            echo $id['patient_id'];
                        }
                    }
                }
  • 写回答

3条回答 默认 最新

  • dongweihuai5601 2010-10-02 12:47
    关注

    Furthering @DarkDust's answer, a fairly efficient way of doing it (assuming you're using raw PHP as opposed to a framework) would be something along the lines of:

    $files = array()
    $rows = array();
    
    while($add = mysql_fetch_array(mysql_query('SELECT filename FROM files')) {
      $rows[] = $add[0]
    }
    
    ...files code
    
    foreach(array_diff($files, $rows) as $deletable) {
      unlink('/path/to/'.$deletable);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建