【问题标题】:Using Logical AND Operators && in foreach php在foreach php中使用逻辑与运算符&&
【发布时间】:2013-03-10 23:09:09
【问题描述】:

我有以下图片库代码:

$directory = 'some path';
$thumbs_directory = 'some path';
foreach (glob($directory.'/*.{jpg,jpeg,png,gif}', GLOB_BRACE) as $file) 
foreach (glob($thumbs_directory.'/*.{jpg,jpeg,png,gif}', GLOB_BRACE) as $file2)
{

    if($file=='.' || $file == '..') continue;
    $file_parts = explode('.',$file);
    $ext = strtolower(array_pop($file_parts));
    $title = basename($file);
    $title = htmlspecialchars($title);
    $title = str_replace("_"," ",$title);
    $nomargin='';
    if(($i+1)%4==0) $nomargin='nomargin';
    echo '
    <div class="pic '.$nomargin.'" style="background:url('.$file2.') no-repeat 50% 50%;">
    <a href="'.$file.'" title="'.$title.'" target="_blank">'.$title.'</a>
    </div>';
    $i++;
}

我需要通过逻辑与运算符 && 组合这些 foreach 语句,以便同时满足两个条件。是否可以 ?我尝试了很多次,但总是以语法错误告终。

请注意,我需要完美定义 $file 和 $file2 变量。这只是缩略图与图像正确关联的唯一方式。

【问题讨论】:

  • 你在找array_merge()
  • 我认为让人们失望的是您反复提到“逻辑与”,而不是您试图在两个文件列表之间维护的实际关系。
  • 我不是核心程序员。所以我不能使用编程术语。

标签: php foreach logical-operators


【解决方案1】:

你能不能简单地将肉肉的逻辑重构为一个通用函数,然后调用它两次?

例如:

function doSomething($directory) {
    foreach (glob($directory.'/*.{jpg,jpeg,png,gif}', GLOB_BRACE) as $file) {
        /* Whatever */
    }
}

...

doSomething($directory);
doSomething($thumbs_directory);

【讨论】:

    【解决方案2】:

    根据您的描述,如果您想遍历两个目录中存在的图像文件,您应该考虑使用 php 的 array_intersect()

    $directory = 'some path';
    $thumbs_directory = 'some path';
    
    $files_in_dir1 = glob($directory.'/*.{jpg,jpeg,png,gif}', GLOB_BRACE);
    $files_in_dir2 = glob($thumbs_directory.'/*.{jpg,jpeg,png,gif}', GLOB_BRACE);
    
    $files_in_both_dirs = array_intersect($files_in_dir1, $files_in_dir2);
    
    foreach ($files_in_both_dirs as $filename) {
      // Code
    }
    

    【讨论】:

    • 我认为您的实际问题是正确的。 OP 正在寻找将图像映射到缩略图上。然而,它可能需要先将两个目录中的基本名称映射到彼此,然后是 array_intersect_assoc 或其他东西。
    【解决方案3】:

    要将图像映射到各自的缩略图,我宁愿选择不同的方法:

    $directory = 'some path';
    $thumbs_directory = 'some path';
    
    // Get all images
    $images = glob($directory.'/*.{jpg,jpeg,png,gif}', GLOB_BRACE);
    
    // Iterate over all images
    foreach ($images as $image) {
        // Construct path to thumbnail
        $thumbnail = $thumbs_directory .'/'. basename($image);
    
        // Check if thumbnail exists
        if (!file_exists($thumbnail)) {
            continue; // skip this image
        }
    
        // .. continue as before
    
        echo '
            <div class="pic '.$nomargin.'" style="background:url('.$thumbnail.') no-repeat 50% 50%;">
            <a href="'.$image.'" title="'.$title.'" target="_blank">'.$title.'</a>
            </div>
        ';
    }
    

    }

    【讨论】:

    • 但我需要完美定义 $file 和 $file2 。请查看我的问题中的代码以获得清晰的视图
    • @rnvipin 没有正确理解您的问题,已编辑我的答案!
    • 谢谢 Niko,我会检查一下 :)
    • 你是最终成功的人 :) 非常感谢!我没有足够的声誉来投票给你的答案 atm,但一旦我有资格,我会确保这样做。再次感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-21
    • 1970-01-01
    相关资源
    最近更新 更多