1

it is easy to read the first value in the array below - but how to read the last value without deleting or looping with e.g. foreach?

$message_rate_array[0]['messages']

sample array (real size is not foreseeable):

Array
(
    [0] => Array
        (
            [messages] => 30584709
            [time] => 1508147394
        )

    [1] => Array
        (
            [messages] => 30585992
            [time] => 1508147395
        )

    [2] => Array
        (
            [messages] => 30587416
            [time] => 1508147396.1
        )

    [3] => Array
        (
            [messages] => 30588721
            [time] => 1508147397.1
        )

)
user8613418
  • 35
  • 1
  • 6
  • 1
    using `end()` you can get the last value of array – Hack it Oct 16 '17 at 10:17
  • 1
    You could `count()` the elements ... – brombeer Oct 16 '17 at 10:18
  • 1
    Have you written any code to try and read that last value? What have you tried? You can't expect SO users to do everything for you. – ajtrichards Oct 16 '17 at 10:18
  • thanks to all for your friendly and fast answers - i was sitting over this question for more than one hour without success - now with your answers it worked instantly! – user8613418 Oct 16 '17 at 10:37
  • and moreover there are so much descriptions with the pros and cons of different solutions you all gave. simply overwhelming! again - thanks to all who spent their time to help me me with this issue :) – user8613418 Oct 16 '17 at 10:48
  • 1
    Possible duplicate of [What's the best way to get the last element of an array without deleting it?](https://stackoverflow.com/questions/3687358/whats-the-best-way-to-get-the-last-element-of-an-array-without-deleting-it) – Ivar Oct 16 '17 at 11:20
  • @Ivar nope - the other thread was 7!!! years old and since some php updates were since then it would have been possible that new functions apply to this question. instead of hunting for duplicates a helpful answer like all the others here gave helped me lot more. – user8613418 Oct 16 '17 at 21:18
  • @user8613418 Does that matter? The most upvoted answer to your question is also an answer of the linked question and your accepted answer has been part of PHP since it came out. And even if there were new ways, then those answers should have been added to the old question. We like to keep all the answers centralized so people with the same question don't have to go through dozens of posts to find the best way. If you were under the impression that we are just here for you, then you are wrong. – Ivar Oct 17 '17 at 07:05

6 Answers6

2

Using end() you can get the last value of array.

Working Demo: https://eval.in/880691

<?php

$people = array("Peter", "Joe", "Glenn", "Cleveland");

echo end($people);

?>

Output: Cleveland

Hack it
  • 405
  • 2
  • 10
1

You can do this:

$last_message = $message_rate_array[count($message_rate_array) - 1]['messages'];
Himanshu Upadhyay
  • 6,558
  • 1
  • 20
  • 33
  • 1
    thank you so much for this perfect and lightning-fast answer. i tried so much for more than one hour but simply was too dumb to find this perfect short and simple solution :) thanx!!! – user8613418 Oct 16 '17 at 10:34
0
$message_rate_array[count($message_rate_array) -1]['messages']

With count() you count the amount of values that are in the array. The - 1 is because the array begins with counting at 0. If the array length is 10, than the array has 9 items(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)

Bian Goole
  • 195
  • 2
  • 13
  • Welcome to Stack Overflow! Thank you for this code snippet, which might provide some limited, immediate help. A proper explanation [would greatly improve](//meta.stackexchange.com/q/114762) its long-term value by showing *why* this is a good solution to the problem, and would make it more useful to future readers with other, similar questions. Please [edit] your answer to add some explanation, including the assumptions you've made. – Toby Speight Oct 16 '17 at 12:45
0

You can use count() to find the length of the array, and subtract one to find the last element.

  $message_rate_array[count($message_rate_array) - 1]['messages']

As a note: you should make sure that array length is greater than zero of you are going to be accessing either the first or last element, so that you can avoid invalid array accessses.

Derek Brown
  • 4,232
  • 4
  • 27
  • 44
0

Refer from http://php.net/manual/en/function.end.php you can do this as the easy and clear way.

$ArrayList = array();
echo end($ArrayList);
M. Pancadewa
  • 145
  • 11
0

You can use end(). It Returns the value of the last element or FALSE for empty array.

<?php   
$lastElement = end($message_rate_array);
print_r($lastElement); 
?>
Farsheel
  • 610
  • 6
  • 17