简体   繁体   English

foreach内部foreach问题

[英]foreach inside foreach issue

I have two directories : one has images and the other has ZIP files. 我有两个目录:一个包含图片,另一个具有ZIP文件。 The files in both directories have the same names eg : 1.zip , 1.png 两个目录中的文件具有相同的名称,例如:1.zip,1.png

I scanned each folder like this : 我这样扫描每个文件夹:

$images    = 'screenshots/';
$scanned_images = array_diff(scandir($images), array('..', '.'));
$zips = 'download/';
$scanned_zips = array_diff(scandir($zips), array('..', '.'));

then : 然后 :

foreach ($scanned_images as $value)
{
echo '<div class="portfolioItem">';
echo '<a href="screenshots/'.$value.'" class="zoom img" title="'.$value.'"      rel="portfolio">';
echo '<img src="screenshots/'.$value.'" class="portfolio-image" alt="'.$value.'" /> </a>';
foreach ($scanned_zips as $val)
{
echo '<div class="portfolioDescription">';
echo'<h4>Download:'.$val.'</h4>';
echo'<p><a href="download/'.$val.'">Click here to download</a></p>';
echo'</div></div>';
}
}

This does not work. 这是行不通的。 Each image in first directory will have the whole zip files of second directory in its description! 第一个目录中的每个图像在其描述中都将包含第二个目录的整个zip文件!

I have also tried to combine the two arrays into one array and use foreach ($result as list ($a, $b)) but as list always gives an error. 我也尝试将两个数组合并为一个数组,并使用foreach ($result as list ($a, $b))但是as list总是会出错。

How to overcome this ? 如何克服呢?

One way is to hash your files by name without extension. 一种方法是按名称对文件进行哈希处理,不带扩展名。 Then use same key to retrieve image data and zip data. 然后使用相同的键检索图像数据和邮政编码数据。 Example: 例:

$scanned_images = array('1.png', '2.png');
$scanned_zips = array('1.zip', '2.zip');

//Should be like that after hashing
$scanned_images = array('1' => '1.png', '2' => '2.png');
$scanned_zips = array('1' => '1.zip', '2' => '2.zip');

So the code could be: 因此代码可能是:

function get_file_name($path) {
    $name = basename($path);
    $name = substr($name, 0, strrpos($name, '.'));
    return $name;
}

function hash_files_by_name($items) {
    $hashed = array();
    foreach($items as $item) {
        $name = get_file_name($item);
        $hashed[$name] = $item;
    }

    return $hashed;
}


$scanned_images = array('1.png', '2.png'); // get images directory filesnames
$scanned_zips = array('1.zip', '2.zip'); // get zips directory filenames.

$imgs = hash_files_by_name($scanned_images);
$zips = hash_files_by_name($scanned_zips);

foreach ($imgs as $key=>$value)
{
    echo '<div class="portfolioItem">';
    echo '<a href="screenshots/'.$value.'" class="zoom img" title="'.$value.'"      rel="portfolio">';
    echo '<img src="screenshots/'.$value.'" class="portfolio-image" alt="'.$value.'" /> </a>';
    if(isset($zips[$key])) {
        echo '<div class="portfolioDescription">';
        echo'<h4>Download:'.$zips[$key].'</h4>';
        echo'<p><a href="download/'.$zips[$key].'">Click here to download</a></p>';
        echo'</div></div>';
    }
}

Add a break; break; statement at the end of your inner foreach loop and that should fix it. 在内部foreach循环末尾的语句,应该可以修复它。 You have two foreach loops and hence the downloads are listed multiple times. 您有两个foreach循环,因此多次列出了下载内容。 To fix this issue, 要解决此问题,

Change your code to: 将您的代码更改为:

<?php
foreach ($scanned_images as $value)
{
echo '<div class="portfolioItem">';
echo '<a href="screenshots/'.$value.'" class="zoom img" title="'.$value.'"      rel="portfolio">';
echo '<img src="screenshots/'.$value.'" class="portfolio-image" alt="'.$value.'" /> </a>';
    foreach ($scanned_zips as $val)
    {
    echo '<div class="portfolioDescription">';
    echo'<h4>Download:'.$val.'</h4>';
    echo'<p><a href="download/'.$val.'">Click here to download</a></p>';
    echo'</div></div>';
    break; //exiting
    }
}

?>

no need to have nested foreach. 无需嵌套foreach。

use this: 用这个:

<?php

$images    = 'screenshots/';
$scanned_images = array_diff(scandir($images), array('..', '.'));
$zips = 'download/';
$scanned_zips = array_diff(scandir($zips), array('..', '.'));

foreach ($scanned_images as $value)
{
    $name = substr($value, 0, strrpos($value, '.'));
    $pos = array_search($name.'.zip', $scanned_zips);
    if($pos != null){
        echo '<div class="portfolioItem">';
        echo    '<a href="'.$images.$value.'" class="zoom img" title="'.$value.'" rel="portfolio">';
        echo        '<img src="'.$images.$value.'" class="portfolio-image" alt="'.$value.'" />';
        echo    '</a>';
        echo    '<div class="portfolioDescription">';
        echo        '<h4>Download:'.$scanned_zips[$pos].'</h4>';
        echo        '<p><a href="'.$zips.$scanned_zips[$pos].'">Click here to download</a></p>';
        echo    '</div>';
        echo '</div>';
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM