Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to unset last item in multidimensional array?

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.

like image 478
Subodh Ghulaxe Avatar asked Dec 18 '25 19:12

Subodh Ghulaxe


2 Answers

Try:

unset ($array_name[count($array_name)-1]);
like image 160
Potheek Avatar answered Dec 20 '25 11:12

Potheek


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

This will pop the last element from each triplet.

like image 38
Maxim Khan-Magomedov Avatar answered Dec 20 '25 10:12

Maxim Khan-Magomedov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!