if(empty($transport[count($transport)-1])) {
unset($transport[count($transport)-1]);
}
Answer from Scott Saunders on Stack OverflowVideos
What happens if you use array_pop on an empty array?
$array = [];
$removed = array_pop($array);
var_dump($removed); How to get the last element of a PHP array?
$array = [10, 20, 30, 40, 50];
$last = array_pop($array);
echo $last;How to remove the last element of a PHP array?
$array = [10, 20, 30, 40];
$removed = array_pop($array);
echo $removed;
print_r($array);
This removes and returns the last item.if(empty($transport[count($transport)-1])) {
unset($transport[count($transport)-1]);
}
The easiest way: array_pop() which will pop an element of the end of the array.
As for the 2nd question:
if (end($transport) == "") {
array_pop($transport);
}
Should handle the second.
EDIT:
Modified the code to conform to the updated information. This should work with associative or indexed based arrays.
Fixed the array_pop, given Scott's comment. Thanks for catching that!
Fixed the fatal error, I guess empty cannot be used with end like I had it. The above code will no longer catch null / false if that is needed you can assign a variable from the end function and test that like so:
$end_item = end($transport);
if (empty($end_item)) {
array_pop($transport);
}
Sorry for posting incorrect code. The above I tested.
Use array_splice():
If you're trying to remove the last n elements, use the following function:
function array_pop_n(array $arr, $n) {
return array_splice($arr, 0, -$n);
}
Demo
If you want to retrieve only the last n elements, then you can use the following function:
function array_pop_n(array $arr, $n) {
array_splice($arr,0,-$n);
return $arr;
}
Demo
It's important to note, looking at the other answers, that array_slice will leave the original array alone, so it will still contain the elements at the end, and array_splice will mutate the original array, removing the elements at the beginning (though in the example given, the function creates a copy, so the original array still would contain all elements). If you want something that literally mimics array_pop (and you don't require the order to be reversed, as it is in your OP), then do the following.
$arr = range(1, 10);
$n = 2;
$popped_array = array_slice($arr, -$n);
$arr = array_slice($arr, 0, -$n);
print_r($popped_array); // returns array(9,10);
print_r($arr); // returns array(1,2,3,4,5,6,7,8);
If you require $popped_array to be reversed, array_reverse it, or just pop it like your original example, it's efficient enough as is and much more direct.
Check out array_slice() http://php.net/manual/en/function.array-slice.php
Last argument true is to preserve keys.
When you pass the offset as negative, it starts from the end. It's a nice trick to get last elements without counting the total.
$array = [
"a" => 1,
"b" => 2,
"c" => 3,
];
$lastElementWithKey = array_slice($array, -1, 1, true);
print_r($lastElementWithKey);
Outputs:
Array
(
[c] => 3
)
try
end($array); //pointer to end
each($array); //get pair