Page 1 of 1

Variable Variables With Associative Arrays

Posted: Wed Apr 25, 2012 10:13 am
by shiznatix
I am trying to write a function that will allow me to get the $_POST value of a variable, even if it is nested in additional arrays within post. For example, I have a form with the following fields:

Code: Select all

<input type="text" name="address[primary][address]" />
<input type="text" name="address[primary][postalCode]" />
<input type="text" name="address[primary][city]" />
<input type="text" name="name" />
<input type="text" name="country" />
As you can see, some will be multi-dimensional while some will not be arrays at all. Easy stuff, but I want to make a function that retrieves that value by only passing the string keys. This is harder than I thought it would be because you have to build the variable name within PHP, not as a string. That is, unless I use variable variables. This is what I am trying to do but I am getting "undefined variable" every time I try it

My function

Code: Select all

public function test()
{
	$args = func_get_args();
	
	$field = '_POST';
	
	foreach ($args as $arg)
	{
		$field .= '["'.$arg.'"]';
	}
	
	dump($_POST["addresses"]["primary"]["address"]);
	dump($field);
	dump(${$field});
	die();
}
This outputs
[text]
--> one //this is correct
--> _POST["addresses"]["primary"]["address"] //this is also correct
--> Notice: Undefined variable: _POST["addresses"]["primary"]["address"]..... //this is NOT correct. This variable obviously does exist!
[/text]

So, does anyone know how to get this to work properly or maybe there is a better way to go about doing this instead of crazy variable variables?

Re: Variable Variables With Associative Arrays

Posted: Wed Apr 25, 2012 10:44 am
by requinix
How about keeping a copy of the "current" array and going into it for each argument?

Code: Select all

public function test() {
	$args = func_get_args();
	$array = $_POST;
	foreach ($args as $arg) {
		if (!is_array($array) || !isset($array[$arg])) return $array;
		$array = $array[$arg];
	}
	return $array;
}
[edit] eval() is a useful function with absolutely no legitimate uses. Including here.

Re: Variable Variables With Associative Arrays

Posted: Wed Apr 25, 2012 10:46 am
by x_mutatis_mutandis_x
You can use "eval()" (See below):

Code: Select all

public function test()
 {
         $args = func_get_args();
         
        //$field = '_POST';
        $field = "$_POST";
         
        foreach ($args as $arg)
         {
                 $field .= '["'.$arg.'"]';
         }
         
        dump($_POST["addresses"]["primary"]["address"]);
         dump($field);
         
         //dump(${$field});
         dump(eval("return $field;"));

         die();
 }

Re: Variable Variables With Associative Arrays

Posted: Wed Apr 25, 2012 10:47 am
by x_mutatis_mutandis_x
requinix wrote:How about keeping a copy of the "current" array and going into it for each argument?

Code: Select all

public function test() {
	$args = func_get_args();
	$array = $_POST;
	foreach ($args as $arg) {
		if (!is_array($array) || !isset($array[$arg])) return $array;
		$array = $array[$arg];
	}
	return $array;
}
Yep a recursive logic will work too, I was just too lazy to write the code for it :P

Re: Variable Variables With Associative Arrays

Posted: Wed Apr 25, 2012 1:12 pm
by shiznatix
Ah, yes, thanks for that. I obviously went with the non-eval solution :) Thanks guys.