Page 1 of 1

Using an array to show the contents of another array

Posted: Sat Feb 02, 2013 7:28 pm
by Lonestarjack
I have a "dump all PHP system variables" program coded in a long program however I think it can be shortened to work by looping through an array of the variable arrays I want to see.
The second foreach does not show the array individual elements

Code: Select all

<?php
	$x = array("\$_GET","\$_POST","\$_REQUEST","\$_SESSION","\$_COOKIE","\$_SERVER");

	foreach ($x as $key=>$val )
		{
        echo '<br>' . $val ;
        echo '<table border="2" style = "background-color:#d4d4d4">';

        	[b]foreach ( $val as $key => $data );[/b]
	       	{
		      	echo "<tr><td style= 'color:red;'>".$key."</td>";
		      	echo "    <td style= 'color:black;'>" .$data."</tr>";
	       	}
	echo"</table>";
        }


Re: Using an array to show the contents of another array

Posted: Sat Feb 02, 2013 7:58 pm
by Benjamin
Maybe this will help:

Code: Select all

/**
 * Returns a dump of an array, object or scalar in <pre> tags.
 *
 * @param mixed $var An array, object or scalar
 * @return string The dump data
 */
function lib_debug($var) {
    if (is_array($var)) {
        return '<pre>' . print_r($var, true) . '</pre>';
    }
    elseif (is_object($var)) {
        return '<pre>' . print_r($var, true) . '</pre>';
    }
    else {
        return '<pre>' . $var . '</pre>';
    }
}

Code: Select all

echo lib_debug($GLOBALS);

Re: Using an array to show the contents of another array

Posted: Sat Feb 02, 2013 8:34 pm
by requinix
For your (Lonestarjack's) script to work it (1) has to use variable variables and (2) needs to account for auto_globals_jit.

1a.

Code: Select all

$x = array("\$_GET","\$_POST","\$_REQUEST","\$_SESSION","\$_COOKIE","\$_SERVER");
If you want to reference a variable then you need to use its name, and the dollar sign is not part of it.

Code: Select all

$x = array("_GET","_POST","_REQUEST","_SESSION","_COOKIE","_SERVER");
1b.

Code: Select all

foreach ( $val as $key => $data );
First take out the semicolon. The $val from the outer foreach loop is just a string: "_GET", "_POST", "_SERVER", etc. You have to dereference the string to get the variable it names.

Code: Select all

foreach ( $$val as $key => $data )
2. When auto_globals_jit is enabled (which it is by default) PHP may not create some of the superglobal arrays until you try to use them directly in code, and variable variables won't trigger their creation.
You can do it easily with something like

Code: Select all

foreach (array($_GET, $_POST, $_REQUEST, $_SESSION, $_COOKIE, $_SERVER) as $array);

Re: Using an array to show the contents of another array

Posted: Mon Feb 04, 2013 11:10 am
by Lonestarjack
Thanks
requinix

Current($arr) gives the first element value instead of the name of the $arr being processed which I would like to capture.
It is handy to include this program when I am debugging a program -- especially when I have to do it on an IPS site.

Code: Select all

<!doctype html>
<?php session_start(); ?> 
<!-- http://127.0.0.1/utility/inc/short_var_dump.php  -->
<html>
<body>
<div align="center">
<?php
    foreach (array($_GET, $_POST, $_REQUEST, $_COOKIE, $_SESSION, $_SERVER) as $arr) {

    echo '<table border="2" style = "background-color:#d4d4d4">';
    echo '<br>' . current($arr) ;
    foreach ($arr as $key=>$data )
	       	{
		      	echo "<tr><td style= 'color:red;'>".$key."</td>";
		      	echo "    <td style= 'color:black;'>" .$data."</tr>";
	       	}
	echo"</table>";
    };
?>

</div>

</body>
</html>


Re: Using an array to show the contents of another array

Posted: Mon Feb 04, 2013 2:22 pm
by requinix
That #2 code I posted was supposed to be taken literally. You used it as the loop when I meant it exactly what it is. All it does is make sure that PHP creates those arrays - besides that "hidden" side-effect the code does absolutely nothing. Intentionally.

To clarify, keep the code you had before and make the changes from #1a and #1b. Then take the code from #2 and put it somewhere earlier in your script than your foreach.

Re: Using an array to show the contents of another array

Posted: Thu Feb 07, 2013 11:00 am
by Lonestarjack
requinix
The question I had really was "How to create a display of the elements of the PHP global variables based on a single array".
I did not accomplish that. Your input was helpful. I am including this code in case someone wants a program to help them debug other programs.

Code: Select all

<?php session_start(); ?>
<!doctype html>
<html>
<body>
<div align="center">
<?php
$count = -1;
$x = array('$_FILES', '$_GET', '$_POST', '$_REQUEST', '$_COOKIE', '$_SESSION', '$_SERVER');

    foreach (array($_FILES, $_GET, $_POST, $_REQUEST, $_COOKIE, $_SESSION, $_SERVER) as $arr){
    $count++;
    if (count($arr)) {
        echo '<table border="2" style = "background-color:#d4d4d4">';
        echo '<tr><th colspan="2">' . $x[$count] . '</th></tr>';
        foreach ($arr as $key=>$data )
	       	{
		      	echo "<tr><td style= 'color:red;'>".$key."</td>";
		      	echo "    <td style= 'color:black;'>" .$data."</tr>";
            };
            	echo "</table><p>";
            } else {
                echo '<table border="2" style = "background-color:#ffff99">';
                echo '<tr><th colspan="2">' . $x[$count] . '</th></tr>';
		      	echo '<tr><td colspan-"2" style= "color:red;">No Elements</td>';
		      	echo '</tr>';
            	echo '</table><p>';
            };
    };
?>
</div>
</body>
</html>

Re: Using an array to show the contents of another array

Posted: Thu Feb 07, 2013 11:20 am
by Weirdan

Re: Using an array to show the contents of another array

Posted: Thu Feb 07, 2013 11:53 am
by Christopher
Lonestarjack wrote:The question I had really was "How to create a display of the elements of the PHP global variables based on a single array".
First, the PHP global variables are in $GLOBALS. I think you are technically talking about PHP's Super Globals.

To combine arrays, not just Super Global arrays, use array_merge():

Code: Select all

$superglobals = array_merge($_FILES, $_GET, $_POST, $_REQUEST, $_COOKIE, $_SESSION, $_SERVER);
foreach ($superglobals as $arr) {
Remember, array_merge() overwrites elements with the same key.