How to determine the first and last iteration in a foreach loop?

前端 未结 20 1348
遇见更好的自我
遇见更好的自我 2020-11-22 16:45

The question is simple. I have a foreach loop in my code:

foreach($array as $element) {
    //code
}

In this loop, I want to r

20条回答
  •  眼角桃花
    2020-11-22 17:23

    You could remove the first and last elements off the array and process them separately.

    Like this:

    
    

    Removing all the formatting to CSS instead of inline tags would improve your code and speed up load time.

    You could also avoid mixing HTML with php logic whenever possible.

    Your page could be made a lot more readable and maintainable by separating things like this:

    
        
      
        
    $item['xcatid'])) ; foreach($subcat as $val) show_subcat($val); ?>
    $c['xcollectionid'])); foreach($cat as $item) show_cat($item); ?>

提交回复
热议问题