1

I have an array like this in php

Array ( [0] => Array ( [month] => April-2014 [total_booking] => 2 ) [1] => Array ( [month] => May-2014 [total_booking] => 5 ) [2] => Array ( [month] => June-2014 [total_booking] => 25 ) [3] => Array ( [month] => October-2013 [total_booking] => 1 ) [4] => Array ( [month] => July-2014 [total_booking] => 4 ) ) 

i want to get the first month value and final value from this array. i am using foreach() for doing this .without using for each is there any good option?

1

3 Answers 3

1

You can get the last element of the array using it's key. In your example they are number, integers so you can just count the array and use the result -1 to get the latest key

echo $array[count($array)-1]['month']; echo $array[count($array)-1]['total_booking']; 
Sign up to request clarification or add additional context in comments.

Comments

1

array_shift($array); returns the first value from array

array_pop($array); returns the last value from array

2 Comments

And modifies the original array. That should be noted.
for that you could create a temporary array
1

Another way:

$first = reset($array)['month']; $last = end($array)['total_booking']; 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.