Try this:
<?php
class Test {
public $arrEgg;
public $arrTypes = array("Egg");
public function __construct() {
foreach($this->arrTypes as $strType) {
if(file_exists("json/$strType.json"))
$this->{'arr' .$strType} = json_decode(file_get_contents("json/$strType.json"), true);
else
echo "File json/$strType.json not found!";
}
}
}
$test = new Test();
echo $test->arrEgg[0][1]['name'];
Output: Egg
Since you want to put data in the $arrEgg
property, you'll want to append the $strType
, such as $this->{'arr' .$strType}
. NOT with $this->arr{$strType}
.
Your JSON is nested, so you'll still need to access it via [0][1]['name']
, unless you change your JSON to be: {"1":{"name":"Test"}}
.