By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,747 Members | 2,011 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,747 IT Pros & Developers. It's quick & easy.

Scope in PHP vs. Python

P: n/a
Hi all,

I have a question regarding scope in PHP. I am attempting navigate a
file structure
a directory using a recursively called function, and all files to an
array. In python, this
goes something like:

=====BEGIN traverse.py =====

def traverseDirectory (path):
files = []

for child in os.listdir(path):
node = os.path.join(path, child)
if os.path.isdir(node):
new = traverseDirectory(node)
files.extend(new)
else:
files.append(node)
return files

=====END traverse.py =====

When I tried to implement this in PHP, however, the array seems to be
overwritten each time:

=====BEGIN traverse.php =====

function traverseDirectory ($path) {
$files = array();

foreach (scandir($path) as $child) {
if (($child != ".") && ($child != "..")) {
$node = $path . DIRECTORY_SEPARATOR . $child;
if (is_dir($node)) {
$new = traverseDirectory($node);
array_merge($files, $new);
}
else {
array_push($files, $node);
}
}
}

return $files;
}

=====END traverse.php =====

What is returned at the end from the PHP version is just an array with
the contents for the last call, but
not for the all calls as it should be.

Any ideas where I'm going wrong? Any suggestions would be greatly
appreciated.

Thanks!
Keith
Aug 13 '08 #1
Share this Question
Share on Google+
4 Replies


P: n/a
..oO(Keith Hughitt)
>When I tried to implement this in PHP, however, the array seems to be
overwritten each time:

=====BEGIN traverse.php =====

function traverseDirectory ($path) {
$files = array();

foreach (scandir($path) as $child) {
if (($child != ".") && ($child != "..")) {
$node = $path . DIRECTORY_SEPARATOR . $child;
if (is_dir($node)) {
$new = traverseDirectory($node);
array_merge($files, $new);
}
else {
array_push($files, $node);
}
}
}

return $files;
}
http://www.php.net/manual/en/function.array-merge.php

| Merges the elements of one or more arrays together so that the values
| of one are appended to the end of the previous one. It returns the
| resulting array. ^^^^^^^

Micha
Aug 13 '08 #2

P: n/a
*** Keith Hughitt escribió/wrote (Wed, 13 Aug 2008 13:45:49 -0700 (PDT)):
function traverseDirectory ($path) {
$files = array();

foreach (scandir($path) as $child) {
if (($child != ".") && ($child != "..")) {
$node = $path . DIRECTORY_SEPARATOR . $child;
if (is_dir($node)) {
$new = traverseDirectory($node);
array_merge($files, $new);
array_merge() doesn't modify its arguments: it returns a new array. You
probably want:

$files = array_merge($files, $new);
}
else {
array_push($files, $node);
On the contrary, array_push() does modify its first argument :)
}
}
}

return $files;


--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://bits.demogracia.com
-- Mi web de humor en cubitos: http://www.demogracia.com
--
Aug 13 '08 #3

P: n/a
Ah. Got it :)
I should have known it would be something like that. I really need to
be more careful
when sifting through the documentation for functions I want to use.

Thank you both for the reply!

Take care,
Keith

On Aug 13, 5:25 pm, Michael Fesser <neti...@gmx.dewrote:
.oO(Keith Hughitt)
When I tried to implement this in PHP, however, the array seems to be
overwritten each time:
=====BEGIN traverse.php =====
function traverseDirectory ($path) {
$files = array();
foreach (scandir($path) as $child) {
if (($child != ".") && ($child != "..")) {
$node = $path . DIRECTORY_SEPARATOR . $child;
if (is_dir($node)) {
$new = traverseDirectory($node);
array_merge($files, $new);
}
else {
array_push($files, $node);
}
}
}
return $files;
}

http://www.php.net/manual/en/function.array-merge.php

| Merges the elements of one or more arrays together so that the values
| of one are appended to the end of the previous one. It returns the
| resulting array. ^^^^^^^

Micha
Aug 14 '08 #4

P: n/a
Keith Hughitt wrote:
On Aug 13, 5:25 pm, Michael Fesser <neti...@gmx.dewrote:
>.oO(Keith Hughitt)
>When I tried to implement this in PHP, however, the array seems to
be overwritten each time:
>=====BEGIN traverse.php =====
function traverseDirectory ($path) {
$files = array();
foreach (scandir($path) as $child) {
if (($child != ".") && ($child != "..")) {
$node = $path . DIRECTORY_SEPARATOR .
$child; if (is_dir($node)) {
$new = traverseDirectory($node);
array_merge($files, $new);
}
else {
array_push($files, $node);
}
}
}
return $files;
}

http://www.php.net/manual/en/function.array-merge.php

| Merges the elements of one or more arrays together so that the
| values of one are appended to the end of the previous one. It
| returns the resulting array.
| ^^^^^^^

Micha
Ah. Got it :)
I should have known it would be something like that. I really need to
be more careful
when sifting through the documentation for functions I want to use.

Thank you both for the reply!

Take care,
Keith
Please do not top post (top posting fixed)
Aug 14 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.