Last active
January 4, 2025 21:28
-
-
Save tannerhodges/7770902 to your computer and use it in GitHub Desktop.
Get the property names of an object. Helpful when parsing JSON data or large arrays with an unknown structure.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Get property names of an object | |
* @return array Property names, else error message. | |
*/ | |
function getPropertyNames($object = null) | |
{ | |
if (!$object) { | |
return array('getPropertyNames() was passed an empty object.'); | |
} | |
$properties = array(); | |
foreach ($object as $property => $property_value) { | |
array_push($properties, $property); | |
} | |
return !empty($properties) ? $properties : array('getPropertyNames() found no properties in your ' . getType($object) . '.'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@jonfriskics True that.
PS - Why doesn't Github Gist send you notifications when people leave comments? Awfully inconvenient to find this a month later.