0

Let's say I have an array array(container, article, title) and a value "Hello, is it me you're looking for?"

Now I want to set the attribute of a given object $target.

Now, what I want to happen is:

//from
$levels = array(container, article, title);
$target->container->article->title = 'Hello World';

// logic
changeAttribute($target, $levels, "Hello, is it me you're looking for?");

//to
echo $target->container->article->title; // 'Hello, is it me you're looking for?';

Is this any way possible? Thanks!

1
  • Whats your question then? Commented Jun 26, 2014 at 19:08

1 Answer 1

1

How about...

function changeAttribute(&$target, $levels, $value) {
    $current = $target;
    foreach($levels as $key) {
      $current =& $current->$key;
    }
    $current = $value;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.