从 PHP 中的关联数组中弹出键和值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6608934/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:50:28  来源:igfitidea点击:

Popping the key & value from an associative array in PHP

phpassociative-array

提问by Pietro Speroni

Let S be an associative array in PHP, I need to retrieve and extract from it the first element, both the value and the key.

让 S 成为 PHP 中的关联数组,我需要从中检索并提取第一个元素,包括值和键。

I would use

我会用

value1=array_pop(S);

but it only gives me the value.

但它只给了我价值。

I can use

我可以用

K=array_keys(S);
key1=array_pop(K);
value1=array_pop(S);

but it is complicated because it requires to have two copies of the same data. WHich is a confusing since the array is itself an element in an array of arrays. There must be a more elegant way to just read the couple key/value while extracting it.

但它很复杂,因为它需要有两个相同数据的副本。这是令人困惑的,因为数组本身就是数组数组中的一个元素。必须有一种更优雅的方法来在提取时读取一对键/值。

回答by hakre

$value = reset($arr);
$key = key($arr);

(in that order)

(以该顺序)

See reset()PHP Manual, key()PHP Manual.

请参阅reset()PHP 手册key()PHP 手册

unset($arr[$key]); # in case you want to remove it.

However array_pop()PHP Manualis working with the last element:

然而,array_pop()PHP 手册正在处理最后一个元素:

$value = end($arr);
$key = key($arr);
unset($arr[$key]); # in case you want to remove it.

See end()PHP Manual.

请参阅end()PHP 手册

For the fun:

为了好玩:

list($value, $key) = array(end($arr), key($arr));

or

或者

extract(array('value'=>end($arr), 'key'=>key($arr)));

or

或者

end($arr);
list($key, $value) = each($arr);

or whatever style of play you like ;)

或任何你喜欢的游戏风格;)

Dealing with empty arrays

处理空数组

It was missing so far to deal with empty arrays. So it's a need to check if there is a last (first) element and if not, set the $keyto null(as nullcan not be an array key):

到目前为止还没有处理空数组。因此,需要检查是否有最后一个(第一个)元素,如果没有,则将其设置$keynull(因为null不能是数组键):

for($key=null;$key===null&&false!==$value=end($arr);)
    unset($arr[$key=key($arr)]);

This will give for a filled array like $arr = array('first' => '1st', 'last' => '2nd.');:

这将提供一个填充数组,如$arr = array('first' => '1st', 'last' => '2nd.');

string(4) "2nd." # value
string(4) "last" # key
array(1) { # leftover array
  ["first"]=>
  string(3) "1st"
}

And an empty array:

和一个空数组:

bool(false) # value
NULL # key
array(0) { # leftover array
}

Afraid of using unset?

害怕使用unset?

In case you don't trust unset()having the performance you need (of which I don't think it's really an issue, albeit I haven't run any metrics), you can use the native array_pop()implementation as well (but I really think that unset()as a language construct might be even faster):

如果您不相信unset()拥有所需的性能(我认为这不是问题,尽管我没有运行任何指标),您也可以使用本机array_pop()实现(但我真的认为这unset()是语言结构可能更快):

end($arr);
$key = key($arr);
$value = array_pop($arr);

回答by OZ_

array_slice

数组切片

$arr = array('k1' => 'v1', 'k2' => 'v2', 'k3' => 'v3');

$a = array_slice($arr, 0, 1);
var_dump($a);

$arr = array_slice($arr, 1);
var_dump($arr);


array(1) {
  ["k1"]=>
  string(2) "v1"
}
array(2) {
  ["k2"]=>
  string(2) "v2"
  ["k3"]=>
  string(2) "v3"
}

回答by Sander Marechal

$value = reset($array);
$key = key($array);

Edit: Hakre just beat me to it :-)

编辑:Hakre 击败了我 :-)

回答by KingCrunch

list($value, $key) = array(reset($s), key($s));
array_shift($s); // or just unset($s[$key]);

Of course you can split the first statement into two separate.

当然,您可以将第一个语句拆分为两个单独的语句。