弹出钥匙 & PHP 中关联数组的值
假设 S 是 PHP 中的关联数组,我需要从中检索并提取第一个元素,包括值和键。
我会使用
value1=array_pop(S);
,但它只给我带来价值。
我可以使用
K=array_keys(S);
key1=array_pop(K);
value1=array_pop(S);
,但它很复杂,因为它需要相同数据的两个副本。这是一个令人困惑的问题,因为数组本身就是数组数组中的一个元素。必须有一种更优雅的方法来在提取键/值时读取它。
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.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
编辑:哈克莱比我先一步:-)
Edit: Hakre just beat me to it :-)
当然,您可以将第一个语句分成两个单独的语句。
Of course you can split the first statement into two separate.
这是我在类似情况下所做的:
尽管我最终使用的解决方案是一个 while 循环(我实际上不需要保留密钥)。
我基本上是试图通过将多个元素放在同一行上来将数组的元素适合到 pdf 的某个区域。我使用 array_chunk() 将数组分成每个不超过 5 个的子数组,然后使用
while(count($chunks) > 0) {}
移动生成的数组。本文的一些解决方案可以类似地在 while 循环中移动/取消设置数组的元素,直到数组为空。Here's what I did in a similar situation:
although the solution I eventually ended up using was a while loop (I didn't really need to retain the keys).
I was basically trying to fit the elements of an array onto an area of a pdf by putting more than one on the same line. I used array_chunk() to break the array up into sub-arrays of 5-or-less each, then shifted the resultant array with a
while(count($chunks) > 0) {}
. Some of the solutions to this post could similarly shift/unset the elements of an array in a while loop until the array was empty.array_slice
array_slice
(按顺序)
参见
reset()
< strong>PHP 手册,key()
PHP 手册。但是
array_pop()
PHP手册正在处理最后一个元素:请参阅
end()
PHP 手册。为了好玩:
(PHP 7.1+)
或
(PHP 4.3+)
或
(PHP 4.3+;警告:extract() 正在使用!)
或
(注意:each() 函数自 PHP 7.2.0 起已弃用,自 PHP 起已消失8.0.0)
或任何你喜欢的游戏风格;)
处理空数组
到目前为止,还缺少处理空数组的功能。因此需要检查是否存在最后一个(第一个)元素,如果没有,则将
$key
设置为null
(因为null
可以不是数组键):这将给出一个填充数组,如
$arr = array('first' => '1st', 'last' => '2nd.');
:和一个空数组:
害怕使用未设置?
如果您不相信
unset()
具有您需要的性能(我认为这不是一个真正的问题,尽管我没有运行任何指标),您可以使用本机array_pop() 也实现了(但我真的认为 unset() 作为语言构造可能会更快):(in that order)
See
reset()
PHP Manual,key()
PHP Manual.However
array_pop()
PHP Manual is working with the last element:See
end()
PHP Manual.For the fun:
(PHP 7.1+)
or
(PHP 4.3+)
or
(PHP 4.3+; Caution: extract() in use!)
or
(Note: The each() function is deprecated since PHP 7.2.0 and gone since PHP 8.0.0)
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
$key
tonull
(asnull
can not be an array key):This will give for a filled array like
$arr = array('first' => '1st', 'last' => '2nd.');
:And an empty array:
Afraid of using 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 nativearray_pop()
implementation as well (but I really think thatunset()
as a language construct might be even faster):