Schneider
Schneider

Reputation: 2496

How to append object name with variable string in php?

I have some array of objects like this

$names=Array ( [0] => stdClass Object ( [id] => 1 
                                        [heading_de] => Title_Deutch 
                                        [heading_en] => Title_English );

And i have some var like

$lang="_en";

When i want to try something like this

foreach ($names as $title)
        {

         $title = $title->heading.$lang;
        }

I only got

$title="_en"

But when i try something like this

foreach ($names as $title)
        {

         $title = $title->heading_en;
        }

I got it ok, where is my mistake, hot to combine string and object ?

Upvotes: 0

Views: 1115

Answers (1)

Victory
Victory

Reputation: 5890

You want to use variable variables in php, try the following

  $lang = "_en";
  foreach ($names as $title) {
    echo $title->{'heading' . $lang};
  }

Upvotes: 4

Related Questions