submitted by: Snippissimo

PHP: get PHP object variables as an array

The get_object_vars() PHP function is exactly what you need to get a list of object variables. This function returns an associative array of defined object properties for the specified PHP object.


code snippet:
<?php
// http://php.snippetdb.com
// Get an associative array of an object's variables using the get_object_vars() command.

// create test object
class toonsObj{
    var 
$mickey 'mouse';
    var 
$donald 'duck';
    var 
$pluto 'dog';
}

//instantiate the variable
$toons = new toonsObj;

//add a new object variable
$toons->porky 'pig';

//return an associative array of object variable names
$vars get_object_vars($toons);

foreach (
$vars as $name=>$val) {
    echo 
"$name is a $val <BR/>";
}
?>

sample output:
mickey is a mouse
donald is a duck
pluto is a dog
porky is a pig