View Full Version : Displaying the name of an array when you don't actually know what it is called...!

03-20-2003, 09:40 PM
Heheh, yup it's an odd one :p

Basically, I have written a nifty little function for debugging, that simply displays all the contents of an array, in a nice little table.

Now, at the top of the table I want to put the name of the array... which is where I run into difficulties!

I can't seem to find any way of actually displaying the variable name... I mean, if I pass $myArray to the function, I want the function to print "myArray" at the top of the table.


Any ideas...? :)

::] krycek [::

03-20-2003, 10:39 PM
Pass the name as a second parameter to the function... or am I missing something?

Since the array parameter your function receives is most certainly named as something - isn't that in conflict with your idea? I mean, when the array is copied into the function's local scope, it is named by the name you gave the parameter - but I guess you wanted the original variable name. But since variables names are not much more than symbols pointing to memory location, I doubt you come much far on this track.

03-21-2003, 01:46 AM
Yeah, I could pass it as a second parameter however I was hoping to do it a little more elegantly :p

I didn't think this was possible (unlike in JavaScript) but I thought I may as well have a go... I could also JUST pass it as text and then evaluate that but that's messy

ah well

thanks for the help :)

::] krycek [::

03-21-2003, 02:10 PM
Originally posted by krycek
I didn't think this was possible (unlike in JavaScript) but I thought I may as well have a go...

How can you do that in JavaScript? I always thought that it wasn't possible as well and would be interested to know how to get the variable's name.

03-21-2003, 02:48 PM
heheh, I figured it out a while back... use the DOM to find the element.name property or some such... ask jkd, I think it was him that helped me figure it out :)

::] krycek [::

03-21-2003, 08:50 PM
It might be so clear to you, but well, I'm not that genius! how can you have the real variable name by sending it as the 2nd parameter?!

Can you bring an example of what you wana do, because basically I think you will call your function in a for loop, right? Something like:

foreach ( $myArr as $key => $val )
myFunction( $val, $key );

That $val is an array itself ( that you do not know it's name ) but $key holds that array's name. It's the only case that I can think of, because if you call an array yourself ( manually! ) then you already know it's name to put as the 2nd argument, huh?