不同的css样式为php数组的最后一个元素 [英] Different css styling for last element of a php array

查看:265
本文介绍了不同的css样式为php数组的最后一个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

循环遍历数组时如何为数组中最后一个要输出的元素创建不同的css div样式。

When looping through an array how can I create a different css div style for the last element to be output in my array.

for($i=0;$i<=count($productid);$i++){if($productrank[$i]>0 ){

<? if (($productid[$i] % 2 ) && !( last element of array){ echo '<div class="centerBoxContentsFeatured centeredContent back  vLine " style="width:50%;">';}
else { echo '<div class="centerBoxContentsFeatured centeredContent back " style="width:50%;">';} ?>


推荐答案

只需检查是否是最后一个$ productid

Just check if it is the last $productid

for(...)
{
    if ($i === (count ($productid) - 1))
        // Last one -> special CSS
    }
}

此外,不要使用 count()在FOR循环中如果你真的不需要。
只需要赋值BEFORE并使用它:

Also, DO NOT use count() in a FOR loop if you don't really have to. Just assign a value BEFORE and use it :

$count_temp  = count ($productid);
for ($i = 0; $i < $count_temp; ++$i)

如果IF语句检查是否是最后一个元素,再次使用$ count_temp

And use this $count_temp again if the IF statement to check if it's the last element

回答注释:

元素?

if ($i === 0)

// Special CSS for $i = 0
// Start loop at 1 instead of 0
for ($i = 1; $i < $count_temp; ++$i)

这篇关于不同的css样式为php数组的最后一个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆