Featured Posts

Writing Website Scrapers in PHPWriting Website Scrapers in PHP This article discusses about how to write a website scraper using PHP for web site data extraction. The concepts taught can be applied and programmed in Java, C#, etc. Basically any language that has a...

Readmore

12 common programming mistakes to avoid12 common programming mistakes to avoid Programming is an art and science and like all art and science the only way to learn is from mistakes. I have made many... and I would like to share with you the mistakes that I have made over my journey...

Readmore

7 habits of highly effective freelance programmers7 habits of highly effective freelance programmers I have developed these based on my freelancing experience. Though I have discontinued freelancing, but would like to share my practices with you. These are basic practices and have been developed over...

Readmore

  • Prev
  • Next

PHP Script to print nested array

Posted on : 16-11-2007 | By : admin | In : PHP, PHP Code Examples

5

If you're new here, you may want to subscribe to my Newsletter. Thanks for visiting!

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




Comments (5)

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

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()?

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.

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

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.”;

}

}

Write a comment

Enter this code