Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP可获得多个结果_Php_Foreach - Fatal编程技术网

PHP可获得多个结果

PHP可获得多个结果,php,foreach,Php,Foreach,我不是一个高级php程序员,所以我需要一些帮助 我试图通过一个指向完整图像和图姆内尔的链接来回应列表中的项目 这是期望的结果: <li> <a href="fullimagepath1"> <img src="thumnailpath1" /> </a> </li> <li> <a href="fullimagepath2"> <img src="th

我不是一个高级php程序员,所以我需要一些帮助

我试图通过一个指向完整图像和图姆内尔的链接来回应列表中的项目

这是期望的结果:

<li>
    <a href="fullimagepath1">
        <img src="thumnailpath1" />
    </a>
</li>
<li>
    <a href="fullimagepath2">
        <img src="thumnailpath2" />
    </a>
</li>
<li>
    <a href="fullimagepath3">
        <img src="thumnailpath3" />
    </a>
</li>
...
  • ...
    这是我正在使用的代码

        <?php
            $images = rwmb_meta( 'product_gallery', 'type=image&size=press-thumb' );
        $fullimages = rwmb_meta( 'product_gallery', 'type=image&size=productfull-thumb' );
                foreach ( $fullimages as $fimages)
                foreach ( $images as $image)
                    {
                        echo "<li><a class='thumb' href='{$fimages['url']}'><img src='{$image['url']}' /></a></li>";
    
        } ?>
    
    
    
    问题是,我得到的是缩略图,但乘以实际结果的数量。如果我的图库有3个缩略图,结果将是9个缩略图,如果我有5个缩略图,结果将是25个

    如何修复代码?
    提前谢谢

    这是因为这行代码触发了内部循环

    由于您可能有3个图像,并且两个数组都包含3个项目数组,所以循环将在一个更大的循环中运行3次,该循环也将执行3次。所以你有9个项目

    在你的代码上

    foreach ( $fullimages as $fimages) //Because of this loop next statement executes
    foreach ( $images as $image) {
      echo "<li><a class='thumb' href='{$fimages['url']}'><img src='{$image['url']}' /></a></li>";
    }
    
    foreach($fullimages as$fimages)//由于这个循环,下一个语句将执行
    foreach($images作为$image){
    回声“
  • ”; }
    你可能想要的是什么

    foreach ( $fullimages as $k => $fimages) {
                          // ^ Get the index of the array
      echo "<li><a class='thumb' href='{$fimages['url']}'>
           <img src='{$images[$k]['url']}' /></a></li>";
                           // ^ Use that key to find the thumbnail from $images array
    }
    
    foreach($k=>fullimages$fimages){
    //^获取数组的索引
    回声“
  • ”; //^使用该键从$images数组中查找缩略图 }
    这是因为这行代码触发了内部循环

    由于您可能有3个图像,并且两个数组都包含3个项目数组,所以循环将在一个更大的循环中运行3次,该循环也将执行3次。所以你有9个项目

    在你的代码上

    foreach ( $fullimages as $fimages) //Because of this loop next statement executes
    foreach ( $images as $image) {
      echo "<li><a class='thumb' href='{$fimages['url']}'><img src='{$image['url']}' /></a></li>";
    }
    
    foreach($fullimages as$fimages)//由于这个循环,下一个语句将执行
    foreach($images作为$image){
    回声“
  • ”; }
    你可能想要的是什么

    foreach ( $fullimages as $k => $fimages) {
                          // ^ Get the index of the array
      echo "<li><a class='thumb' href='{$fimages['url']}'>
           <img src='{$images[$k]['url']}' /></a></li>";
                           // ^ Use that key to find the thumbnail from $images array
    }
    
    foreach($k=>fullimages$fimages){
    //^获取数组的索引
    回声“
  • ”; //^使用该键从$images数组中查找缩略图 }
    试试这段代码

    foreach ( $fullimages as $fimages)
    {
        foreach ( $images as $image)
                    echo "<li><a class='thumb' href='{$fimages['url']}'><img src='{$image['url']}' /></a></li>";
    
    } ?>
    
    foreach($fullimages作为$fimages)
    {
    foreach($images作为$image)
    回声“
  • ”; } ?>
    试试这段代码

    foreach ( $fullimages as $fimages)
    {
        foreach ( $images as $image)
                    echo "<li><a class='thumb' href='{$fimages['url']}'><img src='{$image['url']}' /></a></li>";
    
    } ?>
    
    foreach($fullimages作为$fimages)
    {
    foreach($images作为$image)
    回声“
  • ”; } ?>
    您可以使用

    for ($i=0;$i<count($images);$i++){
        echo $images[$i]['url'], $fullimages[$i]['url'];
    }
    
    您可以使用

    for ($i=0;$i<count($images);$i++){
        echo $images[$i]['url'], $fullimages[$i]['url'];
    }
    

    我建议像这样存储图像:

    $images = array(
     'fullpath' => '...'
     'thumbpath' => '...'    
    )
    
    而不是两个单独的数组。只需在一个数组上循环


    或者使用迭代器函数,如
    next($array)
    current($array)
    在两个数组上同时迭代

    我建议像这样存储图像:

    $images = array(
     'fullpath' => '...'
     'thumbpath' => '...'    
    )
    
    而不是两个单独的数组。只需在一个数组上循环


    或者使用迭代器函数,如
    next($array)
    current($array)
    在两个数组上同时迭代

    你能展示$images和$fullimages包含的内容吗?你能展示$images和$fullimages包含的内容吗?谢谢Starx!它起作用了!我将尝试深入了解它的工作原理。@user2553099,如果您有问题,请告诉我:)谢谢Starx!它起作用了!我将尝试深入了解它是如何工作的。@user2553099,如果您有问题,请告诉我:)