【问题标题】:PHP get value from key value pairPHP从键值对中获取值
【发布时间】: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),我可以获得密钥。是否有类似的功能或方法来查找值?感谢您的帮助!

【问题讨论】:

    标签: php key


    【解决方案1】:

    您可以在循环中定义键。

    foreach ($array as $key => $value) {
      echo $key;
      echo $value;
    }
    

    【讨论】:

      【解决方案2】:

      如果数组中总是有 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.
      

      【讨论】:

        猜你喜欢
        • 2020-08-29
        • 1970-01-01
        • 1970-01-01
        • 2015-02-13
        • 1970-01-01
        • 2019-07-23
        • 1970-01-01
        • 1970-01-01
        • 2015-03-04
        相关资源
        最近更新 更多