Hey.

There is an array of objects

Array ( [0] => stdClass Object ( [param] => vote_date [value] => 2014-09-30 ) [1] => stdClass Object ( [param] => test [value] => no no no ) ) Обходим его foreach($array as $item) { echo $item->param.' => '.$item->value.'<br>'; } 

And now let's imagine that the thing happens inside the class, and the names of the properties of the array objects are set in the class properties

  public $settings; public $field_name = 'param'; public $field_value = 'value'; function __construct($array){ $this->settings = new stdClass(); Обходим массив //этот код вызывает ошибку foreach($array as $item) { $this->settings->$this->field_name = $this->field_value; } } 

Question: how to properly substitute the names of the properties of the object, if they are stored in the properties of the class?

  • Read about the $ operator, it is very useful when you see constructions like: $$$$ name Or even. $$$$$$$$ name Need something like this: $ this -> $ param_name - ReinRaus
  • 3
    I haven't tried it, but it should take off $ this-> settings -> {$ this-> field_name} - etki

0