Loop Through All Properties of a PHP Object

Looping through all of a PHP objects properties is just as easy as an array.

$obj = new stdClass();
$obj->name = 'mike';
$obj->car = true;
$obj->location = 'texas';

foreach ($obj as $key=>$value) {
    echo "$key=>$value\n";
}

Output:

name => mike
car => 1
location => texas

Leave a Reply

Your email address will not be published.