php 数组第一个和最后一个元素,php – 获取数组中的第一个和最后一个元素

reset()和

end()这样做。

从手册:

07002: Returns the value of the first array element, or FALSE if the array is empty.

07003: Returns the value of the last element or FALSE for empty array.

例:

$array = array(24.0,24.1,24.2,24.3,24.4,24.5,24.6);

$first = reset($array);

$last = end($array);

var_dump($first, $last);

?>

哪些输出:

float(24)

float(24.6)

注意:这将重置您的数组指针,意思是如果您使用current()获取当前元素,或者您已经在数组的中间查找,则reset()和end()将重置数组指针(到开始和结束):

$array = array(30.0, 24.0, 24.1, 24.2, 24.3, 24.4, 24.5, 24.6, 12.0);

// reset — Set the internal pointer of an array to its first element

$first = reset($array);

var_dump($first); // float(30)

var_dump(current($array)); // float(30)

// end — Set the internal pointer of an array to its last element

$last = end($array);

var_dump($last); // float(12)

var_dump(current($array)); // float(12) - this is no longer 30 - now it's 12

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值