note 16097 modified in function.array-diff by vrana

From: Date: Tue, 17 Aug 2004 13:57:47 +0000
Subject: note 16097 modified in function.array-diff by vrana
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-79921@lists.php.net to get a copy of this message
I just came upon a really good use for array_diff(). When reading a dir(opendir;readdir), I _rarely_ want "." or ".." to be in the array of files I'm creating. Here's a simple way to remove them: <?php $someFiles = array(); $dp = opendir("/some/dir"); while($someFiles[] = readdir($dp)); closedir($dp); $removeDirs = array(".",".."); $someFiles = array_diff($someFiles, $removeDirs); foreach($someFiles AS $thisFile) echo $thisFile."\n"; ?> S --was-- I just came upon a really good use for array_diff(). When reading a dir(opendir;readdir), I _rarely_ want "." or ".." to be in the array of files I'm creating. Here's a simple way to remove them: $someFiles = array(); $dp = opendir("/some/dir"); while($someFiles[] = readdir($dp)); closedir($dp); $removeDirs = array(".",".."); $someFiles = array_diff($someFiles, $removeDirs); foreach($someFiles AS $thisFile) echo $thisFile."\n"; S http://php.net/manual/en/function.array-diff.php

« previous php.notes (#79921) next »