简体   繁体   English

如何取消设置数组键和值?

[英]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. 如何取消设置最后一个数组,使其必须只显示前2个数组。

please help 请帮忙

thanks 谢谢

If these 3 arrays are the content of one array, let's call it $array : 如果这3个数组是一个数组的内容,那么我们称之为$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 array_pop - 从数组末尾弹出元素

http://php.net/manual/function.array-pop.php 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: 这与unset()的功能相同,但为了好奇,这是另一种方式:

// 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. 只是使用array_pop() ,另一种方法仅用于娱乐目的,但如果你想改变最后一个元素的值,你可以使用它。

Demo: http://codepad.org/UFjal89X 演示: http//codepad.org/UFjal89X

Some reference: 一些参考:

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

end() : http://php.net/manual/function.end.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 工作演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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