【问题标题】:php foreach html output - newbiephp foreach html 输出 - 新手
【发布时间】:2017-10-11 10:51:40
【问题描述】:

抱歉这个非常愚蠢的问题。 这似乎更像是一个逻辑问题而不是编程问题,但无论如何。

所以我有一个很好的数组,我想在 html 中输出,我不想将它分成三个元素的组。

<DIV>
   product1
   product2
   product3
</DIV>
<DIV>
  product4
</DIV>

这是我的代码

 $counter = 0;

        foreach ($prods as $prod) {
            if($counter == 0 || ($counter % 3) == 0) {
                $htm_l .= '<div data-count="' . $counter . '">';
            }

            $htm_l .= '<br> PROD' . $counter;
            $counter++;

            if($counter == 0 || ($counter % 3) == 0) {
                $htm_l .= '</div>';
            }
        }

当然它会产生错误的结果,例如:

<div data-count="0">
    <br> PROD0
    <br> PROD1
    <br> PROD2
</div>
<div data-count="3">
    <br> PROD3

缺少一个结束 div。 什么是实现我的结果的干净方法? 提前致谢

【问题讨论】:

  • 期望的输出是什么?

标签: php html foreach logic


【解决方案1】:

使用下面的代码

$html = '';
$new_array = array_chunk($prods, 3);
$i = 0;
foreach ($new_array as $key => $value) {
    $html .= '<div data-count="'.$i.'">';
        foreach ($value as $ele) {
            $html .= '<br>'.$ele;
            $i++;
        }
    $html .= '</div>';
}

【讨论】:

    【解决方案2】:

    一个快速解决方案在条件后移动$counter++;。您的完整代码将是:

     $counter = 0;
    $htm_l = "";
    
    foreach ($prods as $prod) {
        if($counter == 0 || ($counter % 3) == 0) {
            $htm_l .= '<div data-count="' . $counter . '">';
        }
    
        $htm_l .= '<br> PROD' . $counter;
        if($counter == 0 || ($counter % 3) == 0) {
            $htm_l .= '</div>';
        }
        $counter++;
    
    
    }
    
    echo $htm_l;
    

    【讨论】:

    • 在这种情况下,在第一个周期后,因为计数器为 0,您立即关闭 /div

      PROD0
      。那么它工作正常,我们应该检查它是否真的是第一个循环,但我认为这不是一个好的解决方案
    【解决方案3】:

    $counter=3 之后,它将使用$counter++ 增加1 所以现在$counter=4

    以及您的检查条件

     if($counter == 0 || ($counter % 3) == 0) {
           $htm_l .= '</div>';
     }
    

    它错误并且没有&lt;/div&gt; 分配所以在forloop 结束后你将设置$counter=0 然后检查

    if($counter == 0 || ($counter % 3) == 0) {
           $htm_l .= '</div>';
    }
    

    这样就可以了

    <?php
    $counter = 0;
    $htm_l="";
    foreach ($prods as $prod){
        if($counter == 0 || ($counter % 3) == 0) {
            $htm_l .= '<div data-count="' . $counter . '">';
        }
    
        $htm_l .= '<br> PROD' . $counter;
        $counter++;
    
        if($counter == 0 || ($counter % 3) == 0) {
            $htm_l .= '</div>';
        }
    }
    $counter=0;
    if($counter == 0 || ($counter % 3) == 0) {
        $htm_l .= '</div>';
    }
    echo $htm_l;
    

    【讨论】:

    • 这个很好用,不知道是不是最干净的方法,但确实是个好方法
    【解决方案4】:

    您可能想看看 array_chunk(),它将一个数组分块为具有 X 个元素的数组:https://secure.php.net/manual/en/function.array-chunk.php

    $prods = ['Product1', 'Product2', 'Product3', 'Product4'];
    foreach (array_chunk($prods, 3) as $chunk) {
        echo '<div>' . PHP_EOL;
        foreach ($chunk as $product) {
            echo '<br>' . $product  . PHP_EOL;
        }
        echo '</div>'  . PHP_EOL;
    }
    

    【讨论】:

    • 可能也可以,但是因为在我的情况下 $products 是一个可访问的对象,但严格来说不是一个数组,它不起作用,因为 array_chunk 的第一个参数必须是一个数组。当然我们可以先填充一个数组或者做其他事情。
    猜你喜欢
    • 2018-03-30
    • 1970-01-01
    • 2020-04-27
    • 2014-07-06
    • 1970-01-01
    • 2012-12-18
    • 2011-04-11
    • 2011-10-03
    • 2018-05-16
    相关资源
    最近更新 更多