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
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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

<?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');
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.