0
<ul>
<?php 
foreach (glob("*") as $filename) {
    ?>

  <li><a href="<?php echo $filename;?>"><?php echo $filename; ?></a></li>
<?php 
}
?>
</ul>

I get the output as

home.php
css
contact.php

An so on! My question is can I store all the $filename I get into an array?? Such as

$files=array(home.php,css,contact.php)

If i add new file I need it in an Array! I tried $files=array($filename); but it only make the last file as array!! Anyone can help?? Thanks in advance..

2 Answers 2

2

You need to initialize your array as blank as so:

$files=array();

Then in your loop you need to use array_push() to add the files to the array like this:

array_push($files, $filename);

Once the loop is complete, the array $files will contain all the file names.

Sign up to request clarification or add additional context in comments.

Comments

1

Maybe you want to do something like this?

$filenames = array();           
$all_files = array("home.php", "css", "contact.php");

            foreach ($all_files as $filename) {
                $filenames[] = $filename;
            }

            print_r($filenames);`

Or maybe you want to add all items concatenated, separated by comma?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.