0

Please help in code that i am unable to print values of associative array after extracting itself

class display{
protected $variables = array();
function set($name,$value) {
        $this->variables[$name] = $value;
        }
function render(){                
extract($this->variables);
 // ?? to print values of $variable array
}

3 Answers 3

3
foreach($this->variables as $key => $value) {
  echo "{$key}: {$value}\n";
}
Sign up to request clarification or add additional context in comments.

Comments

1

And how do you try to print the values? The array itself (it's $varables, not $variable, btw) should not be affected.

Update: For what I can tell by your reply to the other answer, you do not really need to extract array. extract jusst puts the variables into local namespace where they will be harder to enumerate. What you need is to use array as is.

foreach($this->variables as $k => $v) echo "$k: $v\n";

or whatever you want to do with them.

Comments

0

if you are using classes, u will need to have something like var $variables = array(); or public $variables = array();

and if you are using structured , you will need to do global $variables; inside the function .. but as u are using $this-> it indicates ur using a class. You will have to put in some more code in here to make the situation clear.

3 Comments

Thanks for giving information , and i am giving exact situation in coding class dispaly{ protected $variables = array() function set($name,$value) { $this->variables[$name] = $value; } function render(){ extract($this->variable); // here i want to print values of $variable array } } Please help me really i am strucking with this..
extract($this->variable); should be extract($this->variables);
so from what i understand: $this->variables['k1'] = 'hello'; $this->variables['k2'] = 'world'; extract($this->variables); echo $k1; /// prints hello echo $k2; /// prints world is that not happening?

Your Answer

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