foreach内部的foreach问题

时间:2013-07-14 16:47:50

标签: php foreach scandir

我有两个目录:一个有图像,另一个有ZIP文件。 两个目录中的文件具有相同的名称,例如:1.zip,1.png

我扫描了每个文件夹:

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

然后:

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>';
}
}

这不起作用。第一个目录中的每个图像都将在其描述中包含第二个目录的完整zip文件!

我还尝试将两个数组合并为一个数组并使用foreach ($result as list ($a, $b)),但as list总是会出错。

如何克服这个问题?

3 个答案:

答案 0 :(得分:0)

一种方法是按名称散列您的文件而不使用扩展名。然后使用相同的密钥来检索图像数据和zip数据。例如:

$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');

所以代码可以是:

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>';
    }
}

答案 1 :(得分:0)

在内部foreach循环的末尾添加break;语句,应该修复它。您有两个foreach循环,因此会多次列出下载。要解决此问题,

将您的代码更改为:

<?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
    }
}

?>

答案 2 :(得分:0)

无需嵌套foreach。

使用它:

<?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>';
    }
}