Reputation: 18661
I have a array like below
Array
(
[0] => Array
(
[0] => Date
[1] => Name
[2] => Hours
)
[1] => Array
(
[0] => 2013-01-02
[1] => Test User
[2] => 7:59
)
[2] => Array
(
[0] => 2013-01-03
[1] => Test User
[2] => 7:53
)
[3] => Array
(
[0] => 2013-01-04
[1] => Test User
[2] => 8:12
)
.
.
.
.
[16] => Array
(
[0] =>
[1] => Total
[2] => 103:1
)
[17] => Array
(
[0] =>
)
)
And want to remove last item from array, I have tried array_pop but this is not working after passing above array to array_pop gives me output
Array
(
[0] =>
)
How can I achieve this with minimum code.
Upvotes: 2
Views: 5673
Reputation: 47991
You are seeing the "popped" element instead of the modified array.
array_pop()
returns the data in the element that it removes from the array.
This means that you wrote:
print_r(array_pop($array));
Instead, modify the array with array_pop()
, then print the array.
array_pop($array);
print_r($array);
Upvotes: 0
Reputation: 868
if all your arrays are indexed by numbers from zero to max without any breaks, you can use
unset($ar[count($ar)-1]);
otherwise try
end($ar);
unset($ar[key($ar)]);
Upvotes: 0
Reputation: 1336
$callback = function(&$array) { array_pop($array); };
array_walk($array, $callback);
This will pop the last element from each triplet.
Upvotes: 3