Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return a key=>value pair from a function in PHP

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 key($result[0]) or an array with two values, as I would like something with a syntax or structure that suggests the key=>value relationship between the values. Is there a more elegant alternative to the array for returning multiple values from a function in php?

like image 414
Sinthia V Avatar asked Jan 28 '26 07:01

Sinthia V


1 Answers

You can only use array(), really.

return array("key1" => "value1", "key2" => "value2");

Anything else would just be uglier or more confusing.

like image 181
Polynomial Avatar answered Jan 30 '26 22:01

Polynomial



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!