On Wed, Sep 3, 2014 at 11:23 AM, Peter Ford <pete@xxxxxxxxxxxxx> wrote: > I came across a bug in some of my code, where I do something like the > following: > > $obj = new MyObject(); > > foreach ($obj as $key=>$value) > > { > > echo $key; > > } > > > Note that I don't use $value in the loop - I'm only doing processing based > on the field name. > Now my IDE flags this unused variable as a hint, so (on autopilot) I > changed it to be > > $obj = new MyObject(); > foreach (array_keys($obj) as $key) > { > echo $key; > } > > Which removes the hint, and looks like the right sort of thing. > Of course this doesn't work: "array_keys() expects parameter 1 to be an > array, object found". > > So what is the correct way to list the field names of an object? > > Cheers > Pete > > -- > Peter Ford phone: 01580 893333 fax: 01580 893399 > Justcroft International Ltd. > www.justcroft.com > Justcroft House, High Street, Staplehurst, Kent TN12 0AH United Kingdom > Registered in England and Wales: 2297906 > Registered office: Stag Gates House, 63/64 The Avenue, Southampton SO17 1XS > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > You could use get_object_vars http://php.net/manual/en/function.get-object-vars.php So something like the following may work (not tested) $obj = new MyObject(); foreach (array_keys(get_object_vars($obj)) as $key) { echo $key; } Regards, Shaun