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

时间:2022-01-29 21:20:32

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”)元素。问候

4 个解决方案

#1


76  

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.

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

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

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

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()来获取当前元素,或者您已经搜索到数组的中间,则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

#2


6  

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) - 1 to get the last item.

您可以使用方括号语法访问数组元素。因此要首先使用0,因为数组是从零开始索引并计数($ arr) - 1来获取最后一项。

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

#3


1  

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:

并结束()得到最后一个:

$lastEle = end($arr);

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

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

#4


0  

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

#1


76  

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.

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

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

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

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()来获取当前元素,或者您已经搜索到数组的中间,则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

#2


6  

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) - 1 to get the last item.

您可以使用方括号语法访问数组元素。因此要首先使用0,因为数组是从零开始索引并计数($ arr) - 1来获取最后一项。

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

#3


1  

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:

并结束()得到最后一个:

$lastEle = end($arr);

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

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

#4


0  

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