-1

I have keys and values as follows :

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

I use this to make objects with $this from the above arrays

foreach($above_array as $key => $value){
  $this->$key = $value;
}

echo $this->store_name; // this does not work, please help

1 Answers1

0

You cannot use $this->, if you are not inside a class:

<?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; 

?>

Demo

syck
  • 2,857
  • 1
  • 10
  • 20