28

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

CharlesB
  • 80,832
  • 27
  • 184
  • 208
user723220
  • 787
  • 3
  • 11
  • 20

5 Answers5

84

You could easily do it by type casting the object:

$keys = array_keys((array)$BIRD);
Soviut
  • 83,904
  • 44
  • 175
  • 239
brenjt
  • 15,581
  • 13
  • 78
  • 116
48

Similar to brenjt's response, this uses PHP's get_object_vars instead of type casting the object.

$array = get_object_vars($object);
$properties = array_keys($array);
nick
  • 2,680
  • 1
  • 19
  • 17
13

If the 'object' is actually an associative array rather than a true object then array_keys() will give you what you need without warnings or errors.

On the other hand, if your object is a true object, then you will get a warning if you try use array_keys() directly.

You can extract the key-value pairs from an object as an associative array with get_object_vars(), you can then get the keys from this with array_keys():

$keysFromObject = array_keys(get_object_vars($anObject));
Bart B
  • 650
  • 9
  • 18
2

Looks like array_keys might have stopped working on objects but, amazingly, the foreach construct works, at least on a php stdClass object.

$object = new stdClass();
$object->a = 20;
$object->b = "hello";
$keys = array_keys($object);
// array_keys returns null. PHP Version 7.3.3 windows
foreach($object as $key=>$value)
{
     // but this works
     echo("key:" . $key . " value:" . $value . "\n");
}
-11

I could be wrong but try to use array_keys using a object as parameter. I believe that is possible in php. http://php.net/manual/en/function.array-keys.php

Anyway, read about reflection.

brenjt
  • 15,581
  • 13
  • 78
  • 116
Cloudson
  • 146
  • 5
  • 1
    thanks I've been reading about objects and arrays for the last 3 hours. I just want to know the syntax – user723220 Apr 27 '11 at 12:44
  • is it print BIRD[0]=>[bug]=> or is it print_r BIRD=>0="beetle"=> = I could go on for days and days. Unfortunately there aren't any examples of this on the internet - the closest anyone comes is in this website, another similar question is asked, but he only gives the answer as $$string=>0 ? which fails PHP syntax – user723220 Apr 27 '11 at 12:51
  • var_dump($rss->items); I made it this far! It prints a mish mash of strings from the object, but I can't seem to continue the equation to the text node. – user723220 Apr 27 '11 at 13:18
  • Ok I found a workaround, in case you need values from an object, just `$var_info = print_r($rss->items, true);` where $rss is the object name and items is the child array - then use string functions to extract the text you need. – user723220 Apr 27 '11 at 13:31
  • If you need help printing keys from an object, check out the documentation that comes with Simple_DOM, shows you how to print the various keys in Simple_DOM objects which you can apply towards your Objects. – user723220 Apr 28 '11 at 22:57
  • 4
    array_keys works only with arrays. If you try with an object you will get a warning "Warning: array_keys() expects parameter 1 to be array, object given" – Oussama May 14 '19 at 14:51
  • 1
    I think it is wrong to use object in function that expect array. This should not be accepted answer. – zanvidmar Jul 13 '19 at 10:41