Variable Variables With Associative Arrays

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Variable Variables With Associative Arrays

Post 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?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Variable Variables With Associative Arrays

Post 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.
Last edited by requinix on Wed Apr 25, 2012 11:14 am, edited 1 time in total.
x_mutatis_mutandis_x
Forum Contributor
Posts: 160
Joined: Tue Apr 17, 2012 12:57 pm

Re: Variable Variables With Associative Arrays

Post 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();
 }
x_mutatis_mutandis_x
Forum Contributor
Posts: 160
Joined: Tue Apr 17, 2012 12:57 pm

Re: Variable Variables With Associative Arrays

Post 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
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Re: Variable Variables With Associative Arrays

Post by shiznatix »

Ah, yes, thanks for that. I obviously went with the non-eval solution :) Thanks guys.
Post Reply