PHP使用string作为名称创建变量

I'm stuck for 2 hours now trying to create variables dynamically using names from an array. What am I missing?

private $filter_keys = array(
  'filter_warranty_id',
  'filter_service_centre_id'
}

foreach($this->filter_keys as $filter) {
  $($filter) => 'bla'
}

I've looked into variable variables, extract, compact, but none of these seem to get the job done.

There are some errors in the code -

  • Unexpected }
  • missing ;
  • Syntax error $($filter) => 'bla'

The code should be -

private $filter_keys = array(
  'filter_warranty_id',
  'filter_service_centre_id'
);

foreach($this->filter_keys as $filter) {
  $$filter = 'bla';
}

var_dump($filter_warranty_id); 

Output

string(3) "bla"

try this :

foreach($this->filter_keys as $filter) {
    $$filter = 'bla';
}