php convert array to object

An array can be converted to an object in PHP.

<?php

$a=array("aa","bb");

$a=(object)$a

?>

However you can not access the member of $a because there is not a name for the member of converted object. If you convert an array of  type key=>value (so called associative array) to an object, you will be able to access its members(properties) by their names which are the keys for the original array.

<?php

$a=array("aaname"=>"aa","bbname"=>"bb");

$a=(object)$a;

print($a->aaname);

?>

Note that the converted object is of class stdClass.

Posted in

Leave a Reply