Return to Snippet

Revision: 5316
at February 27, 2008 14:36 by dbug13

Initial Code
 * Note This has been tested on PHP5 
 * I haven't tried it on PHP4
$arr = array(
	"name" => "Jamie Allison",
	"age" => "31"
// This is all that is required to cast an array to an object.
$data = (object)$arr;

// Next create an object manually, with the same data. 
// For comparison purposes below.
$test = new StdClass;
$test->name = "Jamie Allison";
$test->age = "31";

// Print out the Results, first dump the manually created class
print "Manually created StdClass" . PHP_EOL . print_r($test, true) . PHP_EOL;

// Print out the Array Cast as an object.
print "Array cast to an object" . PHP_EOL . print_r($data, true) . PHP_EOL;

Initial URL

Initial Description

Initial Title
Cast an array to an object

Initial Tags
php, textmate

Initial Language