With PHP 4, is there a way to convert an associative array to an object?
For example, suppose I have the following arrays inside a nested associative
array ($nestedAA):
$AA1['field1'] = 'fieldValue1';
$AA2['field2'] = 'fieldValue2';
$nestedAA['AA1'] = $AA1;
$nestedAA['AA2'] = $AA2;
I would like to take $nestedAA and convert it to an object called
$nestedObject with properties I could access like this:
echo $nestedObject->AA1->field1; # This would print 'fieldValue1'
echo $nestedObject->AA2->field2; # This would print 'fieldValue2'
How can I do this? Note, I would like a conversion method that I could use
with any nested associativee array and create the object dynamically (at
runtime).
Thanks,
Robert
For example, suppose I have the following arrays inside a nested associative
array ($nestedAA):
$AA1['field1'] = 'fieldValue1';
$AA2['field2'] = 'fieldValue2';
$nestedAA['AA1'] = $AA1;
$nestedAA['AA2'] = $AA2;
I would like to take $nestedAA and convert it to an object called
$nestedObject with properties I could access like this:
echo $nestedObject->AA1->field1; # This would print 'fieldValue1'
echo $nestedObject->AA2->field2; # This would print 'fieldValue2'
How can I do this? Note, I would like a conversion method that I could use
with any nested associativee array and create the object dynamically (at
runtime).
Thanks,
Robert
Comment