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
Share on other sites

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
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
Link to comment
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.