Mitul Dabhi
answered Nov 30 '-1 00:00
stdClass to Array and Array to stdClass in php ,
stdClass to Array
$data = new stdclass();
$data->name="data";
print_r($data);
$k=get_object_vars($data);
print_r($k);
when you call get_object_vars , it will return array of the object variables ' array which means it convert stdclass to array .
Array to stdClass
function ArrayToObject($ar){
$r=new stdclass();
foreach($ar as $key=>$value){
$r->$key=$value;
}
return $r;
}
$ar=array("data","is","more","important");
$r=ArrayToObject($ar);
print_r($r);
its simple function ArrayToObject which convert array to object means stdClass