【发布时间】:2014-11-15 20:44:19
【问题描述】:
我有一个键值对,我想从中检索值,因为它是循环的一部分,所以键是动态的,我不能通过键名调用值。
我知道如果我有
array (size=1)
'2004-01-07 12:00' => string '12:00 pm 1:30 pm' (length=16)
如果我使用key($array),我可以获得密钥。是否有类似的功能或方法来查找值?感谢您的帮助!
【问题讨论】:
我有一个键值对,我想从中检索值,因为它是循环的一部分,所以键是动态的,我不能通过键名调用值。
我知道如果我有
array (size=1)
'2004-01-07 12:00' => string '12:00 pm 1:30 pm' (length=16)
如果我使用key($array),我可以获得密钥。是否有类似的功能或方法来查找值?感谢您的帮助!
【问题讨论】:
您可以在循环中定义键。
foreach ($array as $key => $value) {
echo $key;
echo $value;
}
【讨论】:
如果数组中总是有 1 个元素 - 或者您总是需要第一个或最后一个元素 - 您可以使用多个数组函数来获取它。
例如:
reset($array) // get the value of the first element
end($array) // get the value of the last element
current($array) // get the value of the current element
// etc.
【讨论】: