How to I get an array of the last n items of another 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.
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).
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.
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.
$n
is equal to the number of items you want off the end.
$arr = array_slice($old_arr, -$n);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With