psorensen
psorensen

Reputation: 827

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!

Upvotes: 1

Views: 7501

Answers (2)

jeroen
jeroen

Reputation: 91762

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.

Upvotes: 3

S.Visser
S.Visser

Reputation: 4725

In a loop you can define the keys.

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

Upvotes: 4

Related Questions