get first and last element in array

后端 未结 6 1953
暗喜
暗喜 2020-12-08 06:30

hey there i have this array:

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

my question:

6条回答
  •  臣服心动
    2020-12-08 07:15

    For first element: current($arrayname);

    For last element: end($arrayname);

    current(): The current() function returns the value of the current element in an array. Every array has an internal pointer to its "current" element, which is initialized to the first element inserted into the array.

    end(): The end() function moves the internal pointer to, and outputs, the last element in the array. Related methods: current() - returns the value of the current element in an array

    $array = array(24.0,24.1,24.2,24.3,24.4,24.5,24.6);
    
    $first = current($array);
    $last = end($array);
    
    echo 'First Element: '.$first.' :: Last Element:'.$last;
    

    Output result:

    First Element: 24 :: Last Element:24.6
    

提交回复
热议问题