Jump to content

set variable name as value of another variable...


Dragen

Recommended Posts

Is it possible to set the name of a variable to the value of another variable.

Lets say I have a variable:

$myvariable = 'hello';

now I want to create another variable with it's own value, but I want the variable to be called whatever $myvariable is equal to.

So in this instance it would look something like this:

$hello = 'somevalue';

 

Is this possible?

I think you missunderstood me..

I don't want to echo two variables.

 

If I have:

$myvariable = 'goldfish';

Then my new variable should be called

$goldfish

or whatever $myvariable is equal to.. if

$myvariable = 'ghdfghjkdf';

then my new variable should read that and call itself $ghdfghjkdf, and have a value which I've designated it.

 

 

EDIT: here's the code I'm trying to put it in:

<?php
function get_variable_arrays($name){
$sql = "SELECT * FROM variables WHERE name = '" . $name . "'";
if($result = mysql_query($sql)){
	if(mysql_num_rows($result) > 0){
		while($row = mysql_fetch_assoc($result)){
			$VARIABLE[] = $row['value']; //I want $VARIABLE to be equal to whatever $name's value is.
		}
	}else{
		echo 'ERROR: array not found!';
	}
}else{
	echo mysql_error();
}
}
?>

Because I don't neccessarily know what $name is going to be, I just want to make the $VARIABLE called whatever $name is.

If I echo $name it will display, for example

helloworld

Then $VARIABLE should be called $helloworld

 

I hope I've explained myself..

no. That's giving $blah the valuse of 'hello world'.

Instead of $blah, I want it to be called 'hello world', or whatever $variable has the value of.

so my two variables would be:

$variable = 'helloworld';

//somehow read the value of $variable and create a new variable with the name
$helloworld = 'another value';

Just out of intrest why do you need this

probably just because I'm an awkward b@stard... ;)

 

I'm just trying out different ideas really.

I use this function several times and wanted to get a different array from it, each time. Instead of them all having the same name's and getting confused with one another, I wanted them to be different arrays.

What I'll probably have to do is have a multi-array..

<?php
function get_variable_arrays($name){
$sql = "SELECT * FROM variables WHERE name = '" . $name . "'";
if($result = mysql_query($sql)){
	if(mysql_num_rows($result) > 0){
		while($row = mysql_fetch_assoc($result)){
			$variable[$name][] = $row['value'];
		}
	}else{
		echo 'ERROR: array not found!';
	}
}else{
	echo mysql_error();
}
}
?>

so have the array $variable, which contains an array with the name of $name.. which contains the $row['value'].

errr.... how would you be able to reference the variables name if you yourself wouldn't know it?

well that's the problem I've got...

 

and why the heck would you need this?

as I said,

probably just because I'm an awkward b@stard... ;)

 

It was just a passing idea...

hmm.. I seem to be having trouble with it.

here's my function:

<?php
function get_variable_arrays($varname){
$sql = "SELECT * FROM variables WHERE `name` = '" . $varname . "'";
if($result = mysql_query($sql)){
	if(mysql_num_rows($result) > 0){
		while($row = mysql_fetch_assoc($result)){
			$$varname[$row['value']] = $row['value']; //here's my variable which takes the value of $varname
		}
	}else{
		echo 'ERROR: array not found!';
	}
}else{
	echo mysql_error();
}
}
?>

And here's how I'm calling it:

<?php
get_variable_arrays('allowtype');
print_r($allowtype);
?>

That should work shouldn't it?

I've set $varname as 'allowtype', which in turn creates the variable $allowtype ans sets it as an array with data on it..

That's I'd hoped anyway.. unfortunatly I get absolutly nothing.

When I print the array it outputs nothing.

 

Have I done something wrong?

It should work as long as it is in the same scope. Remember that in order for a variable to be read somewhere it has to be defined within that scope or defined as global.  I am not sure if this would work but give it a try.

 

<?php
function get_variable_arrays($varname){
$sql = "SELECT * FROM variables WHERE `name` = '" . $varname . "'";
if($result = mysql_query($sql)){
	if(mysql_num_rows($result) > 0){
		while($row = mysql_fetch_assoc($result)){
			 global $$varname;
                                  $$varname[$row['value']] = $row['value']; //here's my variable which takes the value of $varname
                                
		}
	}else{
		echo 'ERROR: array not found!';
	}
}else{
	echo mysql_error();
}
}
?>

 

Unsure if that would work or not.

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.