Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get first and last element in array

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

like image 625
user2999787 Avatar asked Nov 18 '13 16:11

user2999787


People also ask

How do I find the first element of an array?

Alternativly, you can also use the reset() function to get the first element. The reset() function set the internal pointer of an array to its first element and returns the value of the first array element, or FALSE if the array is empty.

How do you access the last element of an array?

1) Using the array length property The length property returns the number of elements in an array. Subtracting 1 from the length of an array gives the index of the last element of an array using which the last element can be accessed.

How can get first and last value in array in PHP?

You can use the end() function in PHP to get the last element of any PHP array. It will set the internal pointer to the last element of the array and return its value.

Which index is the last element in an array called nums at?

6-9-1: Which index is the last element in an array called nums at? Since the first element in an array is at index 0 the last element is the length minus 1. Since the first element in an array is at index 0 the last element is the length minus 1.


1 Answers

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 
like image 146
h2ooooooo Avatar answered Sep 20 '22 06:09

h2ooooooo