PHP Print keys from an object?
You have an object structure where a BIRD entity contains nested arrays indexed by numbers, and each number maps to a sub-key like bug, beetle, or gnat. You want to output this hierarchy with indentation so it looks like:
BIRD
[0]
bug = > value
The confusion usually stems from mixing up procedural arrays, associative arrays, and object properties. In PHP, objects are essentially collections of public and private variables accessed through the -> operator. To print the keys of an object, you need to understand how get_object_vars() works versus how you iterate over a standard array.
If your data structure is defined like this:
class Bird {
public $index = [
0 => ['bug' => 'caterpillar'],
1 => ['beetle' => 'scarab']
];
}
You cannot directly iterate over the object's internal structure using foreach unless you access the property first. The most straightforward approach to print keys is to traverse the properties manually or use reflection if you need deep inspection regardless of visibility.
Using get_object_vars() for Public Properties
For simple cases where you only care about public properties, get_object_vars($bird) returns an associative array of all public properties and their values. You can then loop through that result to print keys:
$bird = new Bird();
$vars = get_object_vars($bird);
foreach ($vars as $key => $value) {
echo "$key\n";
if (is_array($value)) {
foreach ($value as $subKey => $subValue) {
echo " [$subKey]\n";
echo " {$subKey} = > $subValue\n";
}
}
}
This works well when your object is structured with named properties. However, if your data is stored in a single property that holds an array of arrays (as implied by your [0] through [10] example), you are dealing with nested arrays inside an object property.
Traversing Nested Arrays Inside Objects
If your object looks like this:
$bird = (object) [
'data' => [
0 => ['bug' => 'caterpillar'],
1 => ['beetle' => 'scarab']
]
];
You access the nested structure through the property name and then iterate the array. The logic remains standard PHP array traversal, but the entry point is the object:
echo "BIRD\n";
foreach ($bird->data as $index => $subData) {
echo " [$index]\n";
foreach ($subData as $type => $value) {
echo " $type = > $value\n";
}
}
This approach is readable and avoids over-engineering unless you specifically need to hide or expose certain properties.
Reflection for Private or Protected Properties
If the keys you want to print are inside private or protected properties, get_object_vars() won't see them. In that case, you must use ReflectionClass:
$reflector = new ReflectionClass($bird);
foreach ($reflector->getProperties() as $property) {
if (!$property->isPrivate()) {
continue; // Skip private for simplicity, or adjust as needed
}
}
// Or use getPropertyValue and iterate manually
Reflection adds overhead and complexity. Use it only when you truly need to inspect hidden properties. For most logging or debugging tasks, keeping data public or using a dedicated debug method is cleaner.
Why This Matters in Real Applications
Printing object structures is common during debugging or generating reports. However, blindly dumping objects can expose sensitive data or clutter logs with irrelevant noise. Always consider whether the keys you are printing represent user-facing identifiers or internal implementation details. If you are building a Laravel application, you might leverage helpers like dd() for quick inspection, but for production output, format the data explicitly to avoid leaking stack traces or raw variable names that don't match your API contracts.
If you need reusable formatting logic across multiple projects, consider creating a small utility class that handles nested object traversal. This keeps your main application code clean and consistent. For more advanced serialization needs, libraries like symfony/var-dumper offer pretty-printing capabilities that respect type hints and namespaces without manual recursion.