Jump to content

PHP Question about Arrays


Recommended Posts

All-

Here is the simple script I am trying to use :

http://lixlpixel.org/recursive_function/ph...directory_scan/

How do I pull the data out of there? Looks like it is being stored in an array.

I have tried many variations.

Here is some of what I have tried.

$filestructure = scan_directory_recursively('/var/www/');

echo $filestructure['content'{1}];

Can someone please please help me?

Thanks

Defyer!

Link to comment
Share on other sites

Echo will just print the info on the screen saying somethign like "array" but not show you the contents. You can grab individual pieces of the array, but you don't know how many items there are going to be found, or what one you want to look at. The other option is to dump the raw contents. It will look ugly, but it will display the contents of the array, then you can view source of the page, and then see it a little clearer and formatted a tad nicer.

Add somehting like the following to your code at the bottom:

 $fileselection = scan_directory_recursively('./');
 print_r($fileselection);

If all you wanted was a list of files though, you could use the appropriate commands for the OS, like DIR for windows and LS for *nix systems. You can even use "tree".

example for tree in windows:

<textarea cols="80" rows="30">
<?
system ('tree /f /a');
?>
</textarea>

Link to comment
Share on other sites

its really bad practice to use System commands in PHP, but it is an option, just know, that it goes against allowing your code to be ran on all PHP supportive platforms.

If your looking to travers throw the array, then you will want something like this:

$fileselection = scan_directory_recursively('./');
for (i = 0; i < count($fileselection); i++){
echo "$fileselection[i]<br />";
}

Link to comment
Share on other sites

its really bad practice to use System commands in PHP, but it is an option, just know, that it goes against allowing your code to be ran on all PHP supportive platforms.

If your looking to travers throw the array, then you will want something like this:

$fileselection = scan_directory_recursively('./');
for (i = 0; i < count($fileselection); i++){
echo "$fileselection[i]<br />";
}

Didn't seem to work for me. Gets an error. I tried changing it to this:

$fileselection = scan_directory_recursively('./');
for ($i = 0; $i < count($fileselection); $i++){
echo "$fileselection[$i]<br />";
}

but then all I get is the word "array" repeated on a new line for each item in the array. Its not exploding/imploding or whatever for the output.

Link to comment
Share on other sites

ah haha, probably should have read through the file a little more.

digip: the issue is, the array returned, is an array of arrays :)

 $fileselection = scan_directory_recursively('./');
 $count = count($fileselection);
 for ($i = 0; $i < $count; $i++) {
    $extractedArray = $fileselection[$i];
    echo $extractedArray['name'] . "<br />";
 }

will return and print all files found in the given directory.

by changing the value in the $extractedArray, we can return 'path', 'name', 'extension' and 'size'.

Deathdefyer2002: all the above posts have been talking about ways to print this out using the echo command...

Link to comment
Share on other sites

Awesome.. That does work... But I was looking at this script to pull all the files recursively. So in other words open up all the folders within that folder and display those files.

I noticed if I change it to 'content' it will give me a bunch of Arrays again. I assume that I need to do another for loop to see the files in there.. but how do I do it?

Also if I change the code to this

$fileselection = scan_directory_recursively('/var/www/Movies/', 'mp4');
$count = count($fileselection);
for ($i = 0; $i < $count; $i++) {
    $extractedArray = $fileselection[$i];
    print_r($extractedArray['content']);
    echo "<br>";

}

It seems to display the full array. If I use an echo instead of a print_r it says array 3 times... the same number of folders i have. I assume I need to do another for loop. But how would I code it?

Any ideas?

I just need to pull the Name Multidimensional array out!

Link to comment
Share on other sites

  • 2 weeks later...
Wouldn't it be easier to use a foreach loop instead of a general for loop?

yea sure... but its all and all really not that much different, and this topic was kinda dead...

anywho

$fileselection = scan_directory_recursively('./');
foreach ($fileselection as $extractedArray) {
    echo $extractedArray['name'] . "<br />";
}

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...