doujing2497 2014-03-21 01:17
浏览 12
已采纳

php foreach首先插入某个类,如果只有一个有不同的包

My images folder has files format like below

    <img src='images/AAA_1.jpg'>
    <img src='images/AAA_2.jpg'>
    <img src='images/AAA_3.jpg'>
    <img src='images/BBB_1.jpg'>
    <img src='images/BBB_2.jpg'>
    <img src='images/BBB_3.jpg'>
    <img src='images/CCC_1.jpg'>
    <img src='images/DDD_1.jpg'>
    <img src='images/EEE_1.jpg'>
    <img src='images/EEE_2.jpg'>
......

my function is

function filter($contain){


    foreach ($carousel as $image) {

        if(strpos($image, $contain) === 0){
            $op = "<div class='box'><img src='imgs/$image'/></div>";
        }
    }

}

getallfiles is to get imgs folder files.

$name has AAA_, BBB_, CCC_, DDD_

How do I insert first class for each group, the output expecting is like below

<div class="box first"> 
    <img src='images/AAA_1.jpg'> 
</div>
<div class="box"> 
    <img src='images/AAA_2.jpg'> 
</div>
<div class="box"> 
    <img src='images/AAA_3.jpg'> 
</div>
<div class="box first"> 
    <img src='images/BBB_1.jpg'> 
</div>
<div class="box"> 
    <img src='images/BBB_2.jpg'> 
</div>
<div class="box"> 
    <img src='images/BBB_3.jpg'> 
</div>
<div class="box first"> 
    <img src='images/CCC_1.jpg'> 
</div>
<div class="box first"> 
    <img src='images/DDD_1.jpg'> 
</div> 
  • 写回答

1条回答 默认 最新

  • doupingtang9627 2014-03-21 01:38
    关注

    You need to set a variable before the loop which will help you determine whether the first for each letter has been set. You output that class the first time, then set that variable to something that will help you know not to output it again.

    Using these arrays:

    $carousel = array(
      'AAA_1.jpg',
      'AAA_2.jpg',
      'AAA_3.jpg',
      'BBB_1.jpg',
      'BBB_2.jpg',
      'BBB_3.jpg',
      'CCC_1.jpg'
    );
    
    $names = array('AAA_', 'BBB_', 'CCC_', 'DDD_');
    

    ... this function:

    function filter($name){
        global $carousel;
        $found_first = false;
        foreach ($carousel as $image) {
           if(strpos($image, $name) === 0){
               $first = !$found_first ? ' first' : ''; // assign class if it's the first
               $found_first = true; // change the variable after the first loop
               $html = "<div class='box%s'><img src='imgs/%s'/></div>" . PHP_EOL;
               echo sprintf($html, $first, $image); // output formatted string
           }
        }
    }
    

    ... and this loop:

    foreach($names as $name) {
        filter($name);
    }
    

    ... you get output like this:

    <div class='box first'><img src='imgs/AAA_1.jpg'/></div>
    <div class='box'><img src='imgs/AAA_2.jpg'/></div>
    <div class='box'><img src='imgs/AAA_3.jpg'/></div>
    <div class='box first'><img src='imgs/BBB_1.jpg'/></div>
    <div class='box'><img src='imgs/BBB_2.jpg'/></div>
    <div class='box'><img src='imgs/BBB_3.jpg'/></div>
    <div class='box first'><img src='imgs/CCC_1.jpg'/></div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大