Question

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.

Was it helpful?

Solution

Try:

unset ($array_name[count($array_name)-1]);

OTHER TIPS

$callback = function(&$array) { array_pop($array); };
array_walk($array, $callback);

This will pop the last element from each triplet.

Try like

$my_cnt = count($my_arr);
unset($my_arr[$my_cnt-1]);

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)]);

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);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top