Jump to content

Walk through all arrays in a script


deadonarrival

Recommended Posts

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

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
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 :) )
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
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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.