将php数组转换为对象$this


Convert php array to object $this

我有如下键和值:

[store_id] => 1
[store_name] => StarShop
[store_phone] => 62-22-8383838
[store_email] => admin@starshop.com

我用这个从上面的数组中制作带有$this的对象

foreach($above_array as $key => $value){
  $this->$key = $value;
}
echo $this->store_name; // this does not work, please help

如果您不在类中,则不能使用$this->

<?php
$a = array(
'store_id' => 1,
'store_name' => 'StarShop',
'store_phone' => '62-22-8383838',
'store_email] => admin@starshop.com',
);
$obj = new StdClass;               // create new object
foreach($a as $key => $value){     // iterate as before
  $obj->$key = $value;             // add properties to object
}
echo $obj->store_name; 
?>

演示