deadonarrival Posted January 26, 2007 Share Posted January 26, 2007 I've got a script, and for debugging purposes I want to be able to walk through every array (well, objects using mysql_fetch_object) in the entire script and pring out the key->value.I know how to do this for each individual array, but I want a small piece of code to do it for me in an include - and the fact that each page has different arrays.Basically is it possible to go through every array and print out the key->value without making manual foreach loops for each one?I'd guess no because it could be used to show sensitive information, but it's worth a try!Any help appreciated<3 jon Link to comment https://forums.phpfreaks.com/topic/35752-walk-through-all-arrays-in-a-script/ Share on other sites More sharing options...
mattd8752 Posted January 26, 2007 Share Posted January 26, 2007 You can echo $array; but you can't do it for every array. Link to comment https://forums.phpfreaks.com/topic/35752-walk-through-all-arrays-in-a-script/#findComment-169438 Share on other sites More sharing options...
trq Posted January 26, 2007 Share Posted January 26, 2007 For starters, objects and array are different so which is it? You can't iterate through objects in php4 only php5.If your just doing this for debugging you might want to take a look at [url=http://php.net/print_r]print_r[/url](). Link to comment https://forums.phpfreaks.com/topic/35752-walk-through-all-arrays-in-a-script/#findComment-169439 Share on other sites More sharing options...
trq Posted January 26, 2007 Share Posted January 26, 2007 [quote]You can echo $array;[/quote]You can't echo an array. Link to comment https://forums.phpfreaks.com/topic/35752-walk-through-all-arrays-in-a-script/#findComment-169440 Share on other sites More sharing options...
kenrbnsn Posted January 26, 2007 Share Posted January 26, 2007 You should be able to use the function [url=http://www.php.net/get_defined_vars]get_defined_vars()[/url] something like this:[code]<?php$def_var = get_defined_vars();foreach ($def_var as $var) if (is_array($var))echo "<pre>$var:" . print_r($var,true) . '</pre>';?>[/code]Note: this is untested.Ken Link to comment https://forums.phpfreaks.com/topic/35752-walk-through-all-arrays-in-a-script/#findComment-169443 Share on other sites More sharing options...
deadonarrival Posted January 26, 2007 Author Share Posted January 26, 2007 Thanks kenrbnsn, brilliant :) ... it brings up every single array, including the php superglobals, but I'm sure that can be sorted. I'll have a play with it later - although if anyone knows an easier way that would be appreciated too.<3(Nb, not gonna set this as solved, because it's not - I'll check back later with what I get :) ) Link to comment https://forums.phpfreaks.com/topic/35752-walk-through-all-arrays-in-a-script/#findComment-169445 Share on other sites More sharing options...
kenrbnsn Posted January 26, 2007 Share Posted January 26, 2007 This is the tested script, which doesn't print the $GLOBALS array:[code]<?php$def_var = get_defined_vars();foreach ($def_var as $var => $x) if (is_array($x) && $var != 'GLOBALS')echo "<pre>$var:" . print_r($x,true) . '</pre>';?>[/code]Ken Link to comment https://forums.phpfreaks.com/topic/35752-walk-through-all-arrays-in-a-script/#findComment-169450 Share on other sites More sharing options...
deadonarrival Posted January 26, 2007 Author Share Posted January 26, 2007 Thanks, although it still brings up a lot of server variables - is this something to do with my PHP settings? Link to comment https://forums.phpfreaks.com/topic/35752-walk-through-all-arrays-in-a-script/#findComment-169660 Share on other sites More sharing options...
kenrbnsn Posted January 26, 2007 Share Posted January 26, 2007 No, that's how it works. If you don't want to see them, make an array that contains the names of the arrays you don't want to see (be sure to include the name of the array you're creating) and only display the arrays found if they are not in this array.For example:[code]<?php$dns = array('dns','_POST','_GET','_REQUEST','GLOBALS','_COOKIE','FILES','HTTP_POST_VARS','HTTP_GET_VARS');$def_var = get_defined_vars();foreach ($def_var as $var => $x) if (is_array($x) && !in_array($var,$dns))echo "<pre>$var:" . print_r($x,true) . '</pre>';?>[/code]Ken Link to comment https://forums.phpfreaks.com/topic/35752-walk-through-all-arrays-in-a-script/#findComment-169705 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.