简体   繁体   中英

how to unset array key and value?

Array
(
    [0] => Array
        (
            [accountNo] => 208773

        )

)
Array
(
    [0] => Array
        (
            [accountNo] => 9415238

        )

)
Array
(
)

how can i unset the last array so that it must display only first 2 array.

please help

thanks

If these 3 arrays are the content of one array, let's call it $array :

array_pop($array);

Will remove the last one, and optionally return it's value.

array_pop — Pop the element off the end of array

http://php.net/manual/function.array-pop.php


This does the same thing as unset() here, but for curiosity's sake, here's another way:

// Move the pointer to the last element
end($array);

// Get the key of the element
$key = key($array);

// Unset the item
unset($array[$key]);

Just use array_pop() though, the other method was for entertainment purposes only, but you could use it if you want to change the last element's value.

Demo: http://codepad.org/UFjal89X

Some reference:

key() : http://php.net/manual/function.key.php

end() : http://php.net/manual/function.end.php

try this ( if i understand your problem)

 $output =array();
 foreach($input as $k=>$v){
    if(!empty($v)){ 
        $output[$k]=$v;
    }
}

WORKING DEMO

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM