52

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

mickmackusa
49k13 gold badges96 silver badges163 bronze badges
asked Nov 18, 2013 at 16:29
0

7 Answers 7

145

reset() and end() does exactly this.

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.

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)

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):

<?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
answered Nov 18, 2013 at 16:32
1
  • yeah but take a look at my array, when i use your code, i get: "Warning: reset() expects parameter 1 to be array, null given" and the same for end() Commented Nov 18, 2013 at 16:40
12

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.

$firstEle = $arr[0];
$lastEle = $arr[count($arr) - 1];
answered May 1, 2015 at 15:53
7

As of PHP 7.3, array_key_first and array_key_last is available

$first = $array[array_key_first($array)]; 
$last = $array[array_key_last($array)];
h2ooooooo
39.6k8 gold badges70 silver badges107 bronze badges
answered Aug 6, 2019 at 16:07
3

You can use reset() to get the first:

$firstEle = reset($arr);

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

And end() to get the last:

$lastEle = end($arr);

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

answered Nov 18, 2013 at 16:32
1

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
answered Nov 19, 2018 at 11:05
1
  • Be aware of using this method. $array = array(1, 2, 3); echo current($array) . " "; echo end($array) . " "; echo current($array) . " "; returns 1 3 3 Commented Jan 18, 2021 at 5:20
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)]; 
?>
answered Feb 2, 2018 at 8:45
0

PHP 8.5 introduces functions for this, further simplifying it:

$first = array_first($array); 
$last = array_last($array);
answered Jul 1 at 3:25
0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.