得到阵列第一和最后一个元素 [英] get first and last element in array

查看:185
本文介绍了得到阵列第一和最后一个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我有此数组:

array(1) {
  ["dump"]=>
  string(38) "["24.0",24.1,24.2,24.3,24.4,24.5,24.6]"
}

我的问题:

如何获得第一,并从该数组的最后一个元素了,所以我将有:

how to get the first and the last element out from this array, so i will have:

$firstEle = "24.0";

$lastEle = "24.6";

有人知道如何从数组的元素?

anybody knows how to get those elements from the array?

我已经尝试过这样的:

$arr = json_decode($_POST["dump"], true); 

$col0 = $arr[0];
$col1 = $arr[1];
$col2 = $arr[2];
$col3 = $arr[3];
$col4 = $arr[4];
$col5 = $arr[5];
$col6 = $arr[6];

我可以选择$ COL0和$ COL6,但数组可能是更长的时间,所以需要一种方法来过滤第一(24.0)和最后一个(24.6)元素。
问候

i could chose $col0 and $col6, but the array could be much longer, so need a way to filter the first("24.0") and the last("24.6") element. greetings

推荐答案

重置 结束 正是这一点。

reset and end does exactly this.

这本手册:

重置():如果数组为空,则返回第一个数组元素的值,或FALSE

reset(): Returns the value of the first array element, or FALSE if the array is empty.

端():返回的最后一个元素的空数组值或FALSE

end(): Returns the value of the last element or FALSE for empty array.

例如:

<?php
    $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);
?>

它输出:

浮动(24)结果
  浮动(24.6)

float(24)
float(24.6)

DEMO

这篇关于得到阵列第一和最后一个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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