- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
PHP print keys from an object?
Let’s say the following is our object −
$employeeDetails = (object) [ 'firstName' => 'John', 'lastName' => 'Doe', 'countryName' => 'US' ];
We want the following output i.e. only the keys −
firstName lastName countryName
To display only the keys from an object, use array_keys() in PHP.
<!DOCTYPE html> <html> <body> <?php $employeeDetails = (object) [ 'firstName' => 'John', 'lastName' => 'Doe', 'countryName' => 'US' ]; $allKeysOfEmployee = array_keys((array)$employeeDetails); echo "All Keys are as follows=","<br>"; foreach($allKeysOfEmployee as &$tempKey) echo $tempKey,"<br>"; ?> </body> </html>
All Keys are as follows= firstName lastName countryName
- Return an array with numeric keys PHP?
- PHP: Remove object from array
- PHP script to get all keys from an array that starts with a certain string
- Print the time an hour ago PHP?
- Search a value in an object with number keys with MongoDB
- Convert object to an array in PHP.
- Convert an object to associative array in PHP
- Reset keys of array elements using PHP ?