Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the last n items in a PHP array as another array?

Tags:

arrays

php

How to I get an array of the last n items of another array in PHP?

like image 409
Dataflashsabot Avatar asked Aug 28 '10 18:08

Dataflashsabot


People also ask

How can we get the last element of an 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.

What is use of array slice in PHP?

The array_slice() function returns selected parts of an array. Note: If the array have string keys, the returned array will always preserve the keys (See example 4).

What does => mean in PHP array?

It means assign the key to $user and the variable to $pass. When you assign an array, you do it like this. $array = array("key" => "value"); It uses the same symbol for processing arrays in foreach statements. The '=>' links the key and the value.

Which of the following PHP array function is used to copy a part of array to another array?

1 Expert Answer Arrays are already assigned by copy. Just assign the array to a variable. $array2 = $array1; If you want them to reference each other (So the change in one array will affect the other) use the & character before the variable.


1 Answers

$n is equal to the number of items you want off the end.

$arr = array_slice($old_arr, -$n); 
like image 179
Tim Cooper Avatar answered Sep 30 '22 21:09

Tim Cooper