# [web] how to get name of an array

## Recommended Posts

arathi    122

##### Share on other sites
Mike.Popoloski    3258
Sounds like he wants reflection. Hurrah for for C# and .NET!

##### Share on other sites
Wan    1366
(assuming the use of PHP here)

You can't. And you shouldn't: the fact that renaming a variable could alter the flow and functionality of your script would be extremely bad practice.

You could encapsulate the arrays in a classes, and use the get_class function to retrieve its type. Or just pass an additional parameter to your function to indicate its meaning. Or even refactor your design to get rid of the need for these 'hacks'.

##### Share on other sites
ToohrVyk    1595
A possible answer (and most certainly the most correct one) would be that since arrays do not, in fact, have names at all, returning the name of an array does not make any sense.

function GetNameOfArray(& $array) { return "array";} Because, after all, the variable$array does reference the array itself just like any other variable would.

The solution in this case is to rely on a sane design for your program, instead of using a name-of-the-variable trick, which would be brittle and difficult to get right. Using classes instead of arrays is a great starting point, for instance.

Quote:
 Original post by Mike.PopoloskiSounds like he wants reflection. Hurrah for for C# and .NET!

Reflection des not support this. This is because a given array (or any object, for that matter) may be referenced by zero, one or several variables at any given time. Returning "the name of the variable" would only work in the case where exactly one variable references the array, which is often not the case here.

##### Share on other sites
Mathachew    352
As pointed out, PHP does not support this. How about passing a second argument?

$customer = array('sefdsf','sdfsdf','sdfsdf');foo($customer, 'customer');function foo($array,$bar) {	switch(\$bar)	{		case 'customer':			// do stuff here			break;		default:			// do stuff here			break;	}}

##### Share on other sites
Mike.Popoloski    3258
Quote:
Original post by ToohrVyk
Quote:
 Original post by Mike.PopoloskiSounds like he wants reflection. Hurrah for for C# and .NET!

Reflection des not support this. This is because a given array (or any object, for that matter) may be referenced by zero, one or several variables at any given time. Returning "the name of the variable" would only work in the case where exactly one variable references the array, which is often not the case here.

Oh. Shows how much I know. I have never really used it to look at fields anyway.