php - Add properties to object using array keys -


Is it possible to build such a way? Say I have such an array:

  $ names = array ('name1', 'name2', 'name3'); $ Value = array ('value1', 'value2', 'value3');   

And then I want to do the following:

  foreach ($ name as $ field) {$ this-> $ Field = $ values ​​[counter counter]; $ Counter ++; }   

So that afterwards, I can use the said object like this:

  $ var1 = $ object-> Name1; $ Var2 = $ object- & gt; Name 2; // output "value1" echo $ var1; // output "value 2" echo $ var2;   

I am an object that is a dynamically designated field. Is it possible with oo PHP?

Yes, you

  $ object = (object) array_combine ($ Name, $ value);   

As suggested by @Sam, works better

Comments

Popular posts from this blog

mysql - BLOB/TEXT column 'value' used in key specification without a key length -

c# - Using Vici cool Storage with monodroid -

python - referencing a variable in another function? -