Sign in to follow this  
arathi

[web] how to get name of an array

Recommended Posts

hi i'm having lots of arrays with values and i have a funtion which does some operations based on the array passed as an argument.. (i.e) i want to check the name of the array and then do operations based on that. So i want to get the name of the array passed into the funtion . for eg: if $cutomer = array('sefdsf','sdfsdf','sdfsdf'); i want to get the value 'customer' is there any function to do this??? please help me..

Share this post


Link to post
Share on other sites
such functionality does probably not exists.
you could get lucky, depending on your language.

you could instead have the first element of the array be the array name, still in my opinion it would look really ugly. you should try to redo those parts that need this functionality so it will work anyway

Share this post


Link to post
Share on other sites
No. Name is not an intrinsic property of a variable, but instead sort of a handle that is (often temporarily) bound to the variable so that it can be accessed. For example,


$myarray = array(1,2,3); // Here the array is named 'myarray'...

function foo($somearray) {
// ...but during the execution of the function call below the same array is called 'somearray'.
}

foo($somearray);




Please tell a bit more about your problem so that we may give a *correct* solution.

Share this post


Link to post
Share on other sites
(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 this post


Link to post
Share on other sites
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.

Anther possible answer would be:
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.Popoloski
Sounds 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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
Quote:
Original post by ToohrVyk
Quote:
Original post by Mike.Popoloski
Sounds 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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this