Foreach源PHP数组如果最后 [英] PHP Foreach If Array Last
本文介绍了Foreach源PHP数组如果最后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
foreach( $tabs2 as $tab2 => $name ){
$class = ( $tab2 == $current ) ? ' current' : '';
echo("<li class='posts'><a href='?page=pigg&tab=help&tab2=$tab2' class='$class'>$name");
echo(' |'); // If array last then do not display
echo('</a></li>');
}
我使用foreach循环来创建一个Word preSS插件我工作的导航,但我不希望'|'要显示的最后一个元素时,code以上就是我这么远,我想使用的注释行的if语句,但不知道最好的方法是什么,什么想法?谢谢!
I'm using a foreach loop to create a navigation for a WordPress plugin I'm working on, but I don't want the ' |' to be displayed for the last element, the code above is what I've got so far, I was thinking of using an if statement on the commented line, but not sure what the best approach would be, any ideas? Thanks!
推荐答案
结束()
功能是你所需要的:
The end()
function is what you need:
if(end($tabs2) !== $name){
echo ' |'; // not the last element
}
这篇关于Foreach源PHP数组如果最后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文