The Identification Number and Password fields need to be filled in. Moreover, you cannot send messages devoid of contents Adding further data is not allowed because this topic has been closed Print You provided no password: you have to write it in the textfield beside the command delete for the topic you want to delete Are you sure you want to delete this comment? You did not cast a vote. You need to cast a vote by selecting a radio button. Impossible to proceed Provide the project number Insufficient form parameters: you forgot to fill in some field/s
 
member picmember pic
 
Snippets of A
 
info Below you can find the text of the snippet you want to read, and the list of the other snippets by this author if available.
What are snippets?
Share on MySpace

Php: Scan A Directory Or An Array Of Arrays (Matrix, Numerical Or Associative)

Snippet: Identification Number »   Snippet: Inclusion syntax »
Visitors: 8,131 Tagged by its author as: Programming Php Characters (in origin): 8,637 (pages: ~ 3)
Author: Em@il Permalink Cast your vote for this topic Printable version
OBJECTIVE: Scan a directory, report all the file names found in the folders, no matter how irregularly these folders may be nested.  
The best solution I found for this task - the fact I crafted this algorithm doesn't mean I judge it an excellent solution without objectivity.  
function scanDirectory($node, $limit=0, $separator="\"){
//validate:
if(!is_dir($node)){return null;}; $newSeparator=($separator=="\")?"/":$separator;
$node=str_replace($separator,$newSeparator,$node); $node=str_replace("//",$newSeparator,$node);
$node=(strrpos($node, $newSeparator)==strlen($node)-1)?
substr($node, 0, strlen($node)-1):$node;
//initialize:
$slashes=substr_count($node, $newSeparator); $limit=(!$limit)?-1:$limit+$slashes;
$stack=array( array($node, $slashes) ); $directories=array(); $files=array(); $output=array();
//RUN:
	while(sizeof($stack) && $stack[0][1]!=$limit){
	$current=array_shift($stack);
	$items=opendir($current[0]);
		while( ($it=readdir($items))!==false ){
			if( strrpos($it,".") === (strlen($it)-1) ){continue;};
		$path=$current[0].$newSeparator.$it;
		$output[]=$path;
			if(is_dir($path)){
			$directories[]=$path; $stack[]=array( $path, substr_count($path, $newSeparator) );}
			else{$files[]=$path;};
		};
	}
return array($output, $directories, $files);
/* keep this comment to reuse freely:
http://www.fullposter.com/?1 */}
Remove colors  
INPUTS: First argument: String, the folder path to start with.  
Second argument, optional: Number, and sets a limit to the depth of the scan.  
 
RETURNS: an array of arrays.  
First nested array: an Array, listing all the folders and directories in the order they have been visited. Since the algorithm adds to the increasing head of the array, the order is reversed: the entries listed with the higher index number, are those visited first, as an upward ladder.  
 
Second nested array: an Array, listing only the directories.  
 
Third: an Array, listing only the files.  
The following version would do the same but not on a directory but on an array of arrays (matrix) no matter how irregularly built and nested; such array (matrix) must be passed as the function first argument in the code below:  
function scanArray($node){
if(!$node || !is_array($node)){return null;};
$output=array();
$stack=array( array($node, sizeof($node), 0, array()) );
	while(sizeof($stack)){
	$currentStackIndex=sizeof($stack)-1;
		if(is_array( $stack[$currentStackIndex][0] ) && $stack[$currentStackIndex][1]){
		$nextObjectIndex=( sizeof($stack[$currentStackIndex][0]) - $stack[$currentStackIndex][1] );
		--$stack[$currentStackIndex][1];
		$stack[$currentStackIndex][2]=1;
			array_push($stack,
				array($stack[$currentStackIndex][0][$nextObjectIndex], sizeof($stack[$currentStackIndex][0][$nextObjectIndex]), 0, array_merge($stack[$currentStackIndex][3], array($nextObjectIndex)))
			);
		continue;};
			if( !$stack[$currentStackIndex][2] ){
				$output[]=array($stack[$currentStackIndex][0], $stack[$currentStackIndex][3]);
			};
		array_pop($stack);
	}
	return $output;
/* keep this comment to reuse freely:
http://www.fullposter.com/?1 */}
Remove colors  
RETURNS: Scans and maps an irregular array. Returns an array, whose each entry is an array of two entries, the first reporting the found edge, the second an array reporting all the numerical indexes that lead there.  
 
The version below does the same as above but on associative arrays namely arrays whose indexes are not numerical:  
function scanAssociative($node){
if(!$node || !is_array($node)){return null;};
$output=array();
$stack=array( array($node, 0, 0, 0, array(), array(), array()) );
	while(sizeof($stack)){
	$currentStackIndex=sizeof($stack)-1;
		if( !$stack[$currentStackIndex][2] && is_array($stack[$currentStackIndex][0]) ){
		$stack[$currentStackIndex][4]=array_keys($stack[$currentStackIndex][0]);
		$stack[$currentStackIndex][1]=sizeof($stack[$currentStackIndex][4]);/*decreasing length*/
		$stack[$currentStackIndex][2]=sizeof($stack[$currentStackIndex][4]);/*fixed length*/
		};
		if(is_array( $stack[$currentStackIndex][0] ) && $stack[$currentStackIndex][1]){
		$nextObjectIndex=( $stack[$currentStackIndex][2] - $stack[$currentStackIndex][1] );
		$nextObjectAssociativeIndex=$stack[$currentStackIndex][4][$nextObjectIndex];
		--$stack[$currentStackIndex][1];
		$stack[$currentStackIndex][3]=1;
			array_push($stack,
				array($stack[$currentStackIndex][0][$nextObjectAssociativeIndex], 0, 0, 0, array(), array_merge($stack[$currentStackIndex][5], array($nextObjectAssociativeIndex)), array_merge($stack[$currentStackIndex][6], array($nextObjectIndex))
				)
			);
		continue;};
			if( !$stack[$currentStackIndex][3] ){
				$output[]=array($stack[$currentStackIndex][0], $stack[$currentStackIndex][5], $stack[$currentStackIndex][6]);
			};
		array_pop($stack);
	}
	return $output;
/* keep this comment to reuse freely:
http://www.fullposter.com/?1 */}
Remove colors  
RETURNS: Scans and maps an irregular associative array. Returns an array, whose each entry is an array of two entries, the first reporting the found edge, the second an array reporting all the numerical indexes that lead there.  
The versions below scan respectively a numerically indexed array and (the second) an associative array.  
Unlike the two previous version these two next functions they do not report only the edges but also the traversed branch objects:  
function scanArrayAll($node){
if(!$node || !is_array($node)){return null;};
$output=array();
$stack=array( array($node, sizeof($node), 0, array()) );
	while(sizeof($stack)){
	$currentStackIndex=sizeof($stack)-1;
		if(is_array( $stack[$currentStackIndex][0] ) && $stack[$currentStackIndex][1]){
				if(!$stack[$currentStackIndex][2]){
				$output[]=array($stack[$currentStackIndex][0], $stack[$currentStackIndex][3]);
				};
		$nextObjectIndex=( sizeof($stack[$currentStackIndex][0]) - $stack[$currentStackIndex][1] );
		--$stack[$currentStackIndex][1];
		$stack[$currentStackIndex][2]=1;
			array_push($stack,
				array($stack[$currentStackIndex][0][$nextObjectIndex], sizeof($stack[$currentStackIndex][0][$nextObjectIndex]), 0, array_merge($stack[$currentStackIndex][3], array($nextObjectIndex)))
			);
		continue;};
			if( !$stack[$currentStackIndex][2] ){
				$output[]=array($stack[$currentStackIndex][0], $stack[$currentStackIndex][3]);
			};
		array_pop($stack);
	}
	return $output;
/* keep this comment to reuse freely:
http://www.fullposter.com/?1 */}
Remove colors  
function scanAssociativeAll($node){
if(!$node || !is_array($node)){return null;};
$output=array();
$stack=array( array($node, 0, 0, 0, array(), array(), array()) );
	while(sizeof($stack)){
	$currentStackIndex=sizeof($stack)-1;
		if( !$stack[$currentStackIndex][2] && is_array($stack[$currentStackIndex][0]) ){
		$stack[$currentStackIndex][4]=array_keys($stack[$currentStackIndex][0]);
		$stack[$currentStackIndex][1]=sizeof($stack[$currentStackIndex][4]);/*decreasing length*/
		$stack[$currentStackIndex][2]=sizeof($stack[$currentStackIndex][4]);/*fixed length*/
		};
		if(is_array( $stack[$currentStackIndex][0] ) && $stack[$currentStackIndex][1]){
							if(!$stack[$currentStackIndex][3]){ 
				$output[]=array($stack[$currentStackIndex][0], $stack[$currentStackIndex][5], $stack[$currentStackIndex][6]); 
				};
		$nextObjectIndex=( $stack[$currentStackIndex][2] - $stack[$currentStackIndex][1] );
		$nextObjectAssociativeIndex=$stack[$currentStackIndex][4][$nextObjectIndex];
		--$stack[$currentStackIndex][1];
		$stack[$currentStackIndex][3]=1;
			array_push($stack,
				array($stack[$currentStackIndex][0][$nextObjectAssociativeIndex], 0, 0, 0, array(), array_merge($stack[$currentStackIndex][5], array($nextObjectAssociativeIndex)), array_merge($stack[$currentStackIndex][6], array($nextObjectIndex))
				)
			);
		continue;};
			if( !$stack[$currentStackIndex][3] ){
				$output[]=array($stack[$currentStackIndex][0], $stack[$currentStackIndex][5], $stack[$currentStackIndex][6]);
			};
		array_pop($stack);
	}
	return $output;
/* keep this comment to reuse freely:
http://www.fullposter.com/?1 */}
Remove colors  
RETURNS: (for both) scans and maps an irregular array. Returns an array, whose each entry is an array of two entries, the first reporting the found edge or traversed branch, the second an array reporting all the numerical indexes that lead there.
Printable version To exclude hyperlinks from the print, check the checkbox »
Rss

Cast your vote for this topic

To perform this operation it is not necessary to be Full Poster members.
«Negative Positive»
Click here to save your vote (Current average: 0.00, Voters: 0)
Are you the author of this topic and do you want to append quickly more text? click here
Other topics
This subscriber has a blog too: Read the blog: 57 Shoutbox: 103 reviews: 153
Visitors: 8,131
Overall visits to all the topics: 5,065,346
Daily average (Calculated from the website subscription day): 2,078.52
Optional sorting commands:
Normal order: click here
Order by amount of visits: click here
Order by category: Programming Php
Current order: normal
Other categories available for this author (Limited data report: 100):
Advice: Martial Arts and Self Defense(4), Books(236), Boxing(2), Critical Reviews and Essays(4), Dictionaries(1), Emergency Care(4), Epistles Letters and Advice(5), Fantasy Epics and Fables(1), History and Documents(5), Humor and Jokes(2), Methematics(2), Music and Lyrics(6), News Digests and Press Reviews(1), Novels Poetry and Stories(3), Philosophy Reviews(7), Poetry(1), Programming(4), Programming Ajax(2), Programming Javascript(82), Programming Php(52), Psychology(1), Quotes(1), Religion Esoterica and Spirituality(13), Scientifical Reviews(4), Self Improvement(1), Sport Activities and Apparels(1), Tarots(1)
Showing topics: 1, 10
Available total: 450
View only a list of the snippets by this author: click here.
Other topics available for this author: click on any title below to view the complete item:
In The Land Of Nod, East Of Eden: Where Fire Does Not Play. A Biblical Exegesis. Identification Number: 467 Visitors: 367 Analisi comparata dei tre pesi massimi con maggior numero di flash ko dal 1969 Identification Number: 466 Visitors: 493 Fighting Competently: Anticipation, And Remember It's In His Eyes Identification Number: 465 Visitors: 629 La Musa Segreta: Superiorità Onnipervasiva Della Boxe Identification Number: 464 Visitors: 1,649 Ha Senso Il Doping Sportivo? Effetto Matteo Nello Sport E Business Sportivo Identification Number: 463 Visitors: 1,640 The Musicians Within The Music Box And Other Hereafter Stories Identification Number: 462 Visitors: 3,070 Freud And Jung In A Nutshell: Three Or So Shots At Psychoanalysis For Dummies Identification Number: 461 Visitors: 4,510 Division The Math Of Gods: Ambiguities Of Antanairesis And New Math Operations Identification Number: 460 Visitors: 4,721 The Meaning Of Cruelty Identification Number: 459 Visitors: 5,278 Dÿanèra Ad Eleusi: La Folgorazione Ontologica: Il Pensare Sistematico E Non Identification Number: 458 Visitors: 6,005
External services
This page of this subscriber uses external services: Hide