Reputation: 827
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
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
Reputation: 4725
In a loop you can define the keys.
foreach ($array as $key => $value) {
echo $key;
echo $value;
}
Upvotes: 4