
- PHP 7 Tutorial
- PHP 7 - Home
- PHP 7 - Introduction
- PHP 7 - Performance
- PHP 7 - Environment Setup
- PHP 7 - Scalar Type Declarations
- PHP 7 - Return Type Declarations
- PHP 7 - Null Coalescing Operator
- PHP 7 - Spaceship Operator
- PHP 7 - Constant Arrays
- PHP 7 - Anonymous Classes
- PHP 7 - Closure::call()
- PHP 7 - Filtered unserialize()
- PHP 7 - IntlChar
- PHP 7 - CSPRNG
- PHP 7 - Expectations
- PHP 7 - use Statement
- PHP 7 - Error Handling
- PHP 7 - Integer Division
- PHP 7 - Session Options
- PHP 7 - Deprecated Features
- PHP 7 - Removed Extensions & SAPIs
- PHP 7 Useful Resources
- PHP 7 - Quick Guide
- PHP 7 - Useful Resources
- PHP 7 - Discussion
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.
Example
<!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>
Output
All Keys are as follows= firstName lastName countryName
- Related Articles
- Return an array with numeric keys PHP?
- JavaScript: replacing object keys with an array
- PHP script to get all keys from an array that starts with a certain string
- PHP: Remove object from array
- Print the time an hour ago PHP?
- JavaScript: How to remove the key-value pairs corresponding to the given keys from an object?
- Search a value in an object with number keys with MongoDB
- How to set dynamic property keys to an object in JavaScript?
- Compare keys & values in a JSON object when one object has extra keys in JavaScript
- Convert object to an array in PHP.
- How to unflatten an object with the paths for keys in JavaScript?
- Recursively list nested object keys JavaScript
- Reset keys of array elements using PHP ?
- Update JavaScript object with another object, but only existing keys?
- Convert an object to associative array in PHP

Advertisements