JustinK101 Posted April 21, 2008 Share Posted April 21, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/102074-using-a-variable-in-another-variable/ Share on other sites More sharing options...
DarkWater Posted April 21, 2008 Share Posted April 21, 2008 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. Umm, ${$array}_preferences[$k] MIGHT work. Not sure. Try it out. Quote Link to comment https://forums.phpfreaks.com/topic/102074-using-a-variable-in-another-variable/#findComment-522555 Share on other sites More sharing options...
JustinK101 Posted April 21, 2008 Author Share Posted April 21, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/102074-using-a-variable-in-another-variable/#findComment-522576 Share on other sites More sharing options...
DarkWater Posted April 21, 2008 Share Posted April 21, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/102074-using-a-variable-in-another-variable/#findComment-522578 Share on other sites More sharing options...
sasa Posted April 21, 2008 Share Posted April 21, 2008 <?php class test{ var $local_preferences = array('l1','l2','l3'); var $remote_preferences = array('r1','r2','r3'); function get($k, $array){ return $this->{$array.'_preferences'}[$k]; } } $a = new test(); echo $b = $a->get(1,'remote'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/102074-using-a-variable-in-another-variable/#findComment-522593 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.