mrherman Posted June 8, 2012 Share Posted June 8, 2012 I've spent a lot of time on this, thinking I could figure it out, but I'm royally stuck. I have a "debugging" helper function that I call when I need to know what is happening to a variable. The results of the call are inserted into a table, which I then review for problems. Here is an earlier version of my function call to "trace_log()." It worked fine. trace_log( file_name(__FILE__), script_name(), __FUNCTION__, __LINE__, var_name(), var_value()); Now I want to put the function call parameters into an array. Here is my code: $myarray = array( 'myfile' => 'file_name(__FILE__)', 'script' => 'script_name()', 'func' => '__FUNCTION__', 'line' => '__LINE__', 'var_name' => 'var_name()', 'var_value' => 'var_value()' ); Note that script_name(), var_name(), and var_value() are user defined functions. Now, I am trying to make the parameters in $myarray work within the function, but I have not been successful. Here is how I think the code should begin: function trace_log( $myarray ) { /* Connect to db */ if( is_array( $myarray ) ) { foreach( $myarray as $k => $v ) { ... !!!!! CAN'T GET PAST THIS POINT !!!!! Here is the code I use in the function "trace_log()" to insert the items into a table: $sql = "INSERT INTO reg_log_trace( step, file, script, func, line, var_name, var_value ) VALUES ( 0,'{$file}','{$script}','{$func}', {$line}, '{$var_name}', '{$var_value}' )"; How can I resolve this? Thanks for the help! Quote Link to comment https://forums.phpfreaks.com/topic/263886-conceptually-stuck-passing-array-as-function-parameter/ Share on other sites More sharing options...
Barand Posted June 8, 2012 Share Posted June 8, 2012 if you want an array as a scalar variable, look at the serialize() function Quote Link to comment https://forums.phpfreaks.com/topic/263886-conceptually-stuck-passing-array-as-function-parameter/#findComment-1352333 Share on other sites More sharing options...
ignace Posted June 9, 2012 Share Posted June 9, 2012 Use extract function trace_log($array) { if (is_array($array)) { extract($array); $sql = "INSERT INTO reg_log_trace( step, file, script, func, line, var_name, var_value ) VALUES ( 0,'{$myfile}','{$script}','{$func}', {$line}, '{$var_name}', '{$var_value}' )"; db_query($sql); } .. } Quote Link to comment https://forums.phpfreaks.com/topic/263886-conceptually-stuck-passing-array-as-function-parameter/#findComment-1352345 Share on other sites More sharing options...
mrherman Posted June 9, 2012 Author Share Posted June 9, 2012 Thanks, guys! Extract() is not working because the array contains various functions. When the array is extract-ed, it echos back the function calls, not the values that they should derive. For example, after extract(), the values are: function trace_log( $a_log ) { if (is_array($a_log)) { extract($a_log); } echo $file . "<br />"; echo $script . "<br />"; echo $func . "<br />"; echo $line . "<br />"; echo $var_name . "<br />"; echo $var_value . "<br />"; ........ Prints out: Notice: Undefined variable: file in E:\Web\HTDOCS\Learnweb\Register_login\includes\inc_functions.php on line 75 script_name() __FUNCTION__ __LINE__ var_name() var_value() As for serialize(), I'm not clear as to how that would be helpful. Quote Link to comment https://forums.phpfreaks.com/topic/263886-conceptually-stuck-passing-array-as-function-parameter/#findComment-1352355 Share on other sites More sharing options...
PFMaBiSmAd Posted June 9, 2012 Share Posted June 9, 2012 it echos back the function calls That's because you have single-quotes around the values when you build the array, making them strings. If you want the values at the time you build the array, remove the single-quotes from around each value. Quote Link to comment https://forums.phpfreaks.com/topic/263886-conceptually-stuck-passing-array-as-function-parameter/#findComment-1352357 Share on other sites More sharing options...
mrherman Posted June 9, 2012 Author Share Posted June 9, 2012 YESSSS!!! That was it!! Thanks for this information. Now everything works! Happy, happy!! Quote Link to comment https://forums.phpfreaks.com/topic/263886-conceptually-stuck-passing-array-as-function-parameter/#findComment-1352359 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.