Jump to content

Using A Variable In Another Variable


JustinK101

Recommended Posts

I am trying to write the following function:

 

public function get($k, $array = "remote") {
		return ($this->$array_preferences[$k]);
	}

 

I don't think this is going to work though, I am trying to use the variable $array to choose the right array. Either:

 

remote_preferences[] or local_preferences[]

 

Thanks.

Link to comment
https://forums.phpfreaks.com/topic/102074-using-a-variable-in-another-variable/
Share on other sites

I am trying to write the following function:

 

public function get($k, $array = "remote") {
		return ($this->$array_preferences[$k]);
	}

 

I don't think this is going to work though, I am trying to use the variable $array to choose the right array. Either:

 

remote_preferences[] or local_preferences[]

 

Thanks.

 

function get_array($k, $array = "remote") {

return ($this->$array[$k])

}

 

What are you using this for?  I could probably code something better if I knew what you wanted it for.  Obviously it's an object though.

 

Edit: OH I GET IT. :o  Umm, ${$array}_preferences[$k] MIGHT work.  Not sure.  Try it out.

darkWater,  nope got a parse error. Basically I am trying to do:

 


get($key, $array) {
   echo $this->$array_preferences[$key];
}

 

I guess I could write functions for each array name, just trying to make one function that is reusable though.

darkWater,  nope got a parse error. Basically I am trying to do:

 


get($key, $array) {
   echo $this->$array_preferences['key'];
}

 

I guess I could write functions for each array name, just trying to make one function that is reusable though.

 

Lol, I thought it'd be a parse error. =(  I had an idea though.  Use a switch statement and just add preference blocks as necessary.

function get($key, $array) {
switch $array {
case "remote":
  return $remote_preferences[$k];
  break;
case "blah":
  return blah;
  break;
}
}

You get the idea.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.