在for循环中,这是一个简单的...
for ( $idx = 0 ; $idx < count ( $array ) ; $idx ++ )
{
if ( $idx == 0 )
{
// This is the first element of the array.
}
}
在foreach循环中如何做到这一点?是否有像is_first()
的函数或类似的东西?我正在寻找像下面这样的东西:
foreach ( $array as $key => $value )
{
if ( /* is the first element */ )
{
// do logic on first element
}
else
{
// all other logic
}
}
我在想我可以设置一个布尔值,如$is_first = true;
,然后一旦循环被迭代一次,将布尔值设置为false。
但是php有很多预先构建的函数,我宁愿使用那些...或者另一种方式...
整个布尔方式似乎几乎像...欺骗:s
干杯,
Alex