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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20052769/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 02:49:19  来源:igfitidea点击:

get first and last element in array

phparraysindexing

提问by user2999787

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:

我的问题:

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

如何从这个数组中取出第一个和最后一个元素,所以我将有:

$firstEle = "24.0";

and

$lastEle = "24.6";

anybody knows how to get those elements from the array?

有人知道如何从数组中获取这些元素吗?

i already tried this:

我已经试过了:

$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];

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

我可以选择 $col0 和 $col6,但数组可能更长,所以需要一种方法来过滤第一个(“24.0”)和最后一个(“24.6”)元素。你好

回答by h2ooooooo

reset()and end()does exactly this.

reset()并且end()正是这样做的。

From the manual:

从手册:

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

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

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

end(): 返回最后一个元素的值,或者为空数组返回 FALSE。

Example:

例子:

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

Which outputs:

哪些输出:

float(24)
float(24.6)

浮动(24)
浮动(24.6)

DEMO

演示



NOTE: This will reset your array pointer meaning if you use current()to get the current element or you've seeked into the middle of the array, reset()and end()will reset the array pointer (to the beginning and to the end):

:如果您使用这将重置数组指针意义current(),以获得当前的元素或者你seeked到阵列的中间,reset()并且end()将重置数组指针(到开头和结尾):

<?php

$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

回答by yckart

You can accessing array elements always with square bracket syntax. So to get the first use 0, as arrays are zero-based indexed and count($arr) - 1to get the last item.

您可以始终使用方括号语法访问数组元素。所以要获得第一次使用0,因为数组是从零开始索引的,并count($arr) - 1获得最后一项。

$firstEle = $arr[0];
$lastEle = $arr[count($arr) - 1];

回答by Vladyslav Panchenko

As of PHP 7.3, array_key_firstand array_key_lastis available

从 PHP 7.3 开始,array_key_firstarray_key_last可用

$first = $array[array_key_first($array)];    
$last = $array[array_key_last($array)];

回答by Gufran Hasan

For first element: current($arrayname);

对于第一个元素: current($arrayname);

For last element: end($arrayname);

对于最后一个元素: 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

current():current() 函数返回数组中当前元素的值。每个数组都有一个指向其“当前”元素的内部指针,该指针被初始化为插入到数组中的第一个元素。

end():end() 函数将内部指针移动到并输出数组中的最后一个元素。相关方法:current()——返回数组中当前元素的值

$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

回答by Kermit

You can use reset()to get the first:

您可以使用reset()来获取第一个:

$firstEle = reset($arr);

reset()rewinds array's internal pointer to the first element and returns the value of the first array element.

reset()倒回数组的内部指针指向第一个元素并返回第一个数组元素的值。

And end()to get the last:

end()获得最后一个:

$lastEle = end($arr);

end()advances array's internal pointer to the last element, and returns its value.

end()将数组的内部指针前进到最后一个元素,并返回其值。

回答by Bachcha Singh

We can acheive the goal using array values and array key also

我们也可以使用数组值和数组键来实现目标

Example : Array Values

示例:数组值

<?php
    $array = array(24.0,24.1,24.2,24.3,24.4,24.5,24.6);        
    $array_values = array_values($array);

    // get the first item in the array
    print array_shift($array_values); 

    // get the last item in the array
    print array_pop($array_values);       
?>

Example : Array Keys

示例:数组键

 <?php
    $array = array(24.0,24.1,24.2,24.3,24.4,24.5,24.6);        
    $array_keys = array_keys($array);

    // get the first item in the array
    print $array[array_shift($array_keys)]; 

    // get the last item in the array
    print $array[array_pop($array_keys)];       
?>