Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Print keys from an object?

I have an object BIRD and then there is [0] through [10] and each number has a subheading like "bug" or "beetle" or "gnat" and a value for each of those.

I want to print

BIRD      [0]        bug = > value  

I can't find out how to do this anywhere - there is talk of PUBLIC and PRIVATE and CLASS and that's where I fall off

like image 794
user723220 Avatar asked Apr 27 '11 12:04

user723220


People also ask

How get the key of an object in PHP?

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. $value = key($myarray);

How do you access the properties of an object in PHP?

The most practical approach is simply to cast the object you are interested in back into an array, which will allow you to access the properties: $a = array('123' => '123', '123foo' => '123foo'); $o = (object)$a; $a = (array)$o; echo $o->{'123'}; // error!

What is array_keys () used for in PHP?

The array_keys() function returns an array containing the keys.


1 Answers

You could easily do it by type casting the object:

$keys = array_keys((array)$BIRD); 
like image 182
brenjt Avatar answered Sep 21 '22 12:09

brenjt