get key object php

I have an object with a single key and its value. But I don”t know the key to access it. What is the most efficient way to get the key without enumerating the object?

So how do you know you've got the right key? – NullUserException Aug 5 “10 at 3:20
Are you looking to do an array_search? – Brandon Horsley Aug 5 “10 at 3:26

I have just a single json object converted to php like and I need to take both, key name and value. – Pablo Aug 5 “10 at 3:29
If you're decoding the JSON yourself, you can decode it into an associative array and make your life slightly easier. json_decode($json, true). – deceze♦ Aug 5 “10 at 3:32
The thing is I really can't change the decode mode. Example I brought was just a leaf in hierarchy. – Pablo Aug 5 “10 at 3:35

If you just want to access the value, you don”t need the key (actually property name) at all:

$value = current((array)$object);

If you really want the property name, try this:

$key = key((array)$object);
I have an array of objects and the objects have one property each, and I don't know the names of the properties. I am iterating through the array using foreach loop, and for each object, I want the key as well as value. I have found this solution, but isn't it inefficient because you are casting the $object to an array each time? (Sorry if it is too basic, but I am a newb. An answer will be greatly appreciated, as I am not sure whether it is wise to use this or not).. – Solace Nov 1 “15 at 22:12
This answer saved my time. Thanks. – Rekha Sep 15 “20 at 5:50

$array = array(“foo” => “bar”); $keys = array_keys($array); echo $keys<0>; // Output: foo


This really works for an object? stdClass::__set_state(array( 'tag1' => 1, )) – Pablo Aug 5 “10 at 3:25
Michael: My mistake, I assumed you were attempting to retrieve the key from an array. In which case, you will want to cast the object to an array most likely. See
thomasrutter's example:… – gpmcadam Aug 5 “10 at 3:27
I was initially misled as well by the use of the word "key", which is an array term… – deceze♦ Aug 5 “10 at 3:30

You can cast the object to an array like this:

$myarray = (array)$myobject;

And then, for an array that has only a single value, this should fetch the key for that value.

Đang xem: Get key object php

$value = key($myarray);

You could do both those in one line if you like. Of course, you could also do it by enumerating the object, like you mentioned in your question.

Xem thêm: An Introduction To Design Patterns In Php — Designpatternsphp 1

To get the value rather than the key, then:

$value = current($myarray);

Thanks for contributing an answer to Stack Overflow!

Please be sure to answer the question. Provide details and share your research!

But avoid

Asking for help, clarification, or responding to other answers.Making statements based on opinion; back them up with references or personal experience.

Xem thêm: date in php w3schools

To learn more, see our tips on writing great answers.

Post Your Answer Discard

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged php object or ask your own question.

What is the correct name for the old green/amber-on-black monitors, and what are the best vintage models to look for used?
Why does C++ not know to do an implicit move in the return when the variable is used in an initializer list?
What is a good replacement for Gaussian? more hot questions
Stack Overflow Questions Jobs Developer Jobs Directory Salary Calculator Help Mobile
Products Teams Talent Advertising Enterprise
Company About Press Work Here Legal Privacy Policy Terms of Service Contact Us Cookie Policy
Stack Exchange
Network Technology Life / Arts Culture / Recreation Science Other
Stack Overflow Server Fault Super User Web Applications Ask Ubuntu Webmasters Game Development
TeX – LaTeX Software Engineering Unix & Linux Ask Different (Apple) WordPress Development Geographic Information Systems Electrical Engineering
Android Enthusiasts Information Security Database Administrators Drupal Answers SharePoint User Experience Mathematica
Salesforce ExpressionEngine® Answers Stack Overflow em Português Blender Network Engineering Cryptography Code Review
Magento Software Recommendations Signal Processing Emacs Raspberry Pi Stack Overflow на русском Code Golf
Stack Overflow en español Ethereum Data Science Arduino Bitcoin Software Quality Assurance & Testing Sound Design
Windows Phone more (28)
Photography Science Fiction & Fantasy Graphic Design Movies & TV Music: Practice & Theory Worldbuilding Video Production
Seasoned Advice (cooking) Home Improvement Personal Finance & Money Academia Law Physical Fitness Gardening & Landscaping
Parenting more (10)
English Language & Usage Skeptics Mi Yodeya (Judaism) Travel Christianity English Language Learners Japanese Language
Chinese Language French Language German Language Biblical Hermeneutics History Spanish Language Islam
Русский язык Russian Language Arqade (gaming) Bicycles Role-playing Games Anime & Manga Puzzling
Motor Vehicle Maintenance & Repair Board & Card Games Bricks Homebrewing Martial Arts The Great Outdoors Poker
Chess Sports more (16)
MathOverflow Mathematics Cross Validated (stats) Theoretical Computer Science Physics Chemistry Biology
Computer Science Philosophy Linguistics Psychology & Neuroscience Computational Science more (10)

Blog Facebook Twitter LinkedIn Instagram

site design / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2021.3.19.38843

Leave a Comment Protection Status