简体   繁体   中英

PHP get value from key value pair

I have a key-value pair from which I would like to retrieve the value and since it's part of a loop, the key is dynamic and I can't call the value by its key name.

I know that if i have

array (size=1)
   '2004-01-07 12:00' => string '12:00 pm 1:30 pm' (length=16)

If I use key($array) I can get the key. Is there an similar function or method to find the value? Appreciate the help!

In a loop you can define the keys.

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

If there always is going to be 1 item in the array - or you always need the first or last element - you can get it with a number of array functions.

For example:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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