Home > PHP, PHP Code Examples > PHP Script to print nested array

PHP Script to print nested array

November 16th, 2007 admin Leave a comment Go to comments

Here is a script that will allow you to print a nested array upto any level. Can I request you to please review this and let me know how this can be further optimized.

$array["sunil"] = "Bhatia";
$array[0] = array("one"=>"two",array(1,2,3,array(4,5,6)));
$array["sunil2"] = "Bhatia2";
 
processArray($array);
 
$count=0;
 
function processArray($array) {
 
	global $count;
 
	$count++; //this variable is for calculating tab space
 
	if(is_array($array) === true) {
 
		foreach($array as $key => $value) {
			if(is_array($array[$key]) === true) {
				processArray($array[$key]);
			}
			else {
				for($i = 1 ; $i < $count ; $i++) echo "&nbsp;&nbsp;"; //change this to space ' ' if running from CLI
				echo $value."<br />";
			}
		}
	}
 
	$count--;
}

Related Posts on PHP5 Tutorial – Object Oriented Programming (OOPS)

  1. PHP5 Tutorial – Learn to create a PHP5 Class
  2. PHP5 Tutorial – Learn to Create a PHP5 Class Object
  3. PHP5 Tutorial – Defining Attributes of a PHP5 Class
  4. PHP5 Tutorial – Defining Methods of a PHP5 Class
  5. PHP5 Tutorial – Creating a PHP5 Constructor __construct()
  6. PHP5 Tutorial OOPS – Creating a PHP5 Destructor __destruct()
  7. PHP5 Tutorial OOPS – PHP5 Class Access Specifiers – public, private and protected
  8. PHP5 Tutorial – $this variable explained
  9. PHP5 Tutorial – instanceOf Operator Explained
  10. PHP5 Tutorial – Defining Class Constants
  11. PHP5 Tutorial – Magic Methods – __toString() method
  12. PHP5 Tutorial – Magic Methods – __get() and __set()
  13. PHP5 Tutorial – Magic Methods – __isset() and __unset()
  14. PHP5 Tutorial – Magic Methods – __call() method
  15. PHP5 Tutorial – Magic Methods – __autoload() method
  16. PHP5 Tutorial – Magic Methods – __sleep() and __wakeup()
  17. PHP5 Tutorial – Magic Methods – __clone() method




Categories: PHP, PHP Code Examples Tags:

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /home/sunilb/www.sunilb.com/wp-includes/l10n.php:142) in /home/sunilb/www.sunilb.com/wp-content/plugins/mycaptcha/MyCaptcha.php on line 41

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/sunilb/www.sunilb.com/wp-includes/l10n.php:142) in /home/sunilb/www.sunilb.com/wp-content/plugins/mycaptcha/MyCaptcha.php on line 41
  1. Stephan
    November 17th, 2007 at 17:11 | #1

    Only had a short look at your code, but I suggest to have a look at “array_walk_recursive” manual

  2. November 17th, 2007 at 20:14 | #2

    I see no problem with it, except for maybe you could do away with printing non-breaking spaces and replace them with the ‘\t’ control character for tabs.

    The for() on line 22 is the only thing that stands out as possibly unnecessary, or as something that could be optimised, but I can’t think of how to do so at the moment so I’ll let it slide. :) hehe.

    I assume you are just trying to do this as a more interface-friendly print_r()?

  3. Stephan
    November 17th, 2007 at 20:40 | #3

    Correction to my previous comment: array_walk_recursive doesn’t exactly help here as it doesn’t work with associative arrays the way I expected.

  4. Swirl
    November 21st, 2007 at 23:28 | #4

    function processArray2($array){
    if (is_array($array)){
    array_map(‘processArray2′, $array);
    } else {
    echo ”.$array;
    }
    }

  5. Swirl
    November 22nd, 2007 at 00:20 | #5

    Delete my comments … forgot about the indents and it doesn’t quite work for the last bit:

    $space = ”;
    processArray2($array);

    function processArray2($array){
    global $space;
    if (is_array($array)){
    $space .= ‘  ’;
    array_map(‘processArray2′, $array);
    } else {
    echo $space.$array.”;

    }

    }

  1. No trackbacks yet.
Enter this code to leave comment (Sorry, but bots get me crazy :) )