I need to return a key value pair from a function. It would be preferable to retain the structure of the data. I would like to avoid creating an array with only one value if possible as I do not care for the syntax Show asked Dec 1, 2011 at 20:41
3 You can only use
Anything else would just be uglier or more confusing. answered Dec 1, 2011 at 20:42
PolynomialPolynomial 26.9k11 gold badges79 silver badges107 bronze badges 1 How about:
Although I must say, this sounds like a bizarrely specific and largely cosmetic requirement... answered Dec 1, 2011 at 20:44
DaveRandomDaveRandom 86.8k11 gold badges149 silver badges173 bronze badges 3 Could you not return the values in an array, like so?
You could also do this:
But that's involves more work to use after the fact, in my opinion. answered Dec 1, 2011 at 20:44
Surreal DreamsSurreal Dreams 25.5k3 gold badges45 silver badges60 bronze badges 1 Return an array.
answered Dec 1, 2011 at 20:43
Madara's GhostMadara's Ghost 168k50 gold badges260 silver badges306 bronze badges Here a full example proposed by @polynomial that might be interesting:
Think this kind of magic only work with PHP 7 or further? OMG! It Works with PHP 5.4! answered Feb 16, 2021 at 23:40
gtamborerogtamborero 2,47723 silver badges23 bronze badges Am I misunderstanding your question, or do you not want answered Dec 1, 2011 at 20:43
simshaunsimshaun 21.1k1 gold badge56 silver badges73 bronze badges 3 (PHP 4, PHP 5, PHP 7, PHP 8) key — Fetch a key from an array Descriptionkey(array|object Parametersarray The array. Return Values The key() function simply returns the key of the array element that's currently being pointed to by the internal pointer. It does not move the pointer in any way. If the internal pointer points beyond the end of the elements list or the array is empty, key() returns ExamplesExample #1 key() example
The above example will output: See Also
lhardie ¶ 8 years ago
vinob44 at gmail dot com ¶ 8 years ago
key($array).'<br />'; FatBat ¶ 10 years ago
Md Tahazzot ¶ 2 years ago
danielmadsv at gmail dot com ¶ 3 years ago
= [ '3' => 14, '1' => 15, '4' => 92, '15' => 65 ];$key_of_max = array_search( max($arr) , $arr);?>
What is array_keys () used for?The array_keys() is a built-in function in PHP and is used to return either all the keys of and array or the subset of the keys. Parameters: The function takes three parameters out of which one is mandatory and other two are optional.
What is $Key in PHP?Technical Details. Return Value: Returns the key of the array element that is currently being pointed to by the internal pointer. PHP Version: 4+
What is the use of array_flip () function?The array_flip() function flips/exchanges all keys with their associated values in an array.
How do you find the key of an array?The array_keys() function is used to get all the keys or a subset of the keys of an array. Note: If the optional search_key_value is specified, then only the keys for that value are returned. Otherwise, all the keys from the array are returned.
|