nikosliapis Posted May 7, 2013 Share Posted May 7, 2013 Hello everyone Let's say that i have a function that does something to a given array: function any_name($array) { do something.... } What i want is a way to "get" the name of the array and echo it out. For example if i use the function as: echo $var = any_name($example1); The output to be: $example1 or echo $var = any_name($example2); The output to be: $example2 Thank you very much for your time. Quote Link to comment https://forums.phpfreaks.com/topic/277747-function-to-get-array-name/ Share on other sites More sharing options...
trq Posted May 7, 2013 Share Posted May 7, 2013 Why would you need to know what your variable is named? Quote Link to comment https://forums.phpfreaks.com/topic/277747-function-to-get-array-name/#findComment-1428877 Share on other sites More sharing options...
nikosliapis Posted May 7, 2013 Author Share Posted May 7, 2013 "trq" thank you for your interest. The "Whole story" is that i'm working on a function that will display a 2-dimension array into a table, so that it would be easy for me to debug any code i write. I need the name of the given array so that i can output it as a title for the generated table. Quote Link to comment https://forums.phpfreaks.com/topic/277747-function-to-get-array-name/#findComment-1428878 Share on other sites More sharing options...
DarkKnight2011 Posted May 7, 2013 Share Posted May 7, 2013 (edited) I would suggest that you use an associative array, ie. $arr = array( 'value1' => 1, 'value2' => 2 ); echo $arr['value1']; // outputs 1 or given your latest comment... $arr = array( 'tableName' => 'Table1', 'tableData => array( 'value1' => 1 ) ); Edited May 7, 2013 by DarkKnight2011 Quote Link to comment https://forums.phpfreaks.com/topic/277747-function-to-get-array-name/#findComment-1428887 Share on other sites More sharing options...
nikosliapis Posted May 7, 2013 Author Share Posted May 7, 2013 "darkknight" thank you for your answer but I trully can't understand how it will help me to what I'm looking for.Perhaps if you would explain it to me. Quote Link to comment https://forums.phpfreaks.com/topic/277747-function-to-get-array-name/#findComment-1428929 Share on other sites More sharing options...
requinix Posted May 7, 2013 Share Posted May 7, 2013 It helps because what you're literally asking for is impossible. You need to find another way - like with an associative array. Quote Link to comment https://forums.phpfreaks.com/topic/277747-function-to-get-array-name/#findComment-1428944 Share on other sites More sharing options...
akphidelt2007 Posted May 7, 2013 Share Posted May 7, 2013 You can add the variable name to the function... Like function any_name($example1,'example1') {} Quote Link to comment https://forums.phpfreaks.com/topic/277747-function-to-get-array-name/#findComment-1428948 Share on other sites More sharing options...
AbraCadaver Posted May 7, 2013 Share Posted May 7, 2013 Along the same lines: $test_array = range(1,10); test(compact('test_array')); function test($arg) { $name = key($arg); $array = $arg[$name]; echo $name; print_r($array); } Quote Link to comment https://forums.phpfreaks.com/topic/277747-function-to-get-array-name/#findComment-1428958 Share on other sites More sharing options...
nikosliapis Posted May 7, 2013 Author Share Posted May 7, 2013 Thank you all for your answers. "requinix" you trully "clean things up", so i'll just go along the solution "akphidedelt2007" proposed. Again thank you all. Quote Link to comment https://forums.phpfreaks.com/topic/277747-function-to-get-array-name/#findComment-1428982 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.