tags:

views:

110

answers:

3

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
}
+2  A: 
foreach($this->variables as $key => $value) {
  echo "{$key}: {$value}\n";
}
Jeff Ober
+1  A: 

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.

Michael Krelin - hacker
A: 

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.

Sabeen Malik
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);
Sabeen Malik
so from what i understand:$this->variables['k1'] = 'hello';$this->variables['k2'] = 'world';extract($this->variables);echo $k1; /// prints helloecho $k2; /// prints worldis that not happening?
Sabeen Malik