Solved

iterate through all the elements in an array instead of print_r

Posted on 2013-01-15
3
424 Views
Last Modified: 2013-01-15
<?php

$filesarray = array();

if ($handle = opendir('.')) {
    while (false !== ($entry = readdir($handle))) {
        if ($entry != "." && $entry != ".." && is_file($entry)) {
            //echo "$entry<br />";
            $filesarray[] = $entry;
        }
    }
    closedir($handle);
}

print_r($filesarray);
?>

Open in new window


this question is just for the last line:
there are elements in

print_r($filesarray);

and I want to do a foreach or while loop to iterate through all the elements and echo each element
0
Comment
Question by:rgb192
3 Comments
 
LVL 4

Expert Comment

by:rahulgupta9x
Comment Utility
0
 
LVL 12

Accepted Solution

by:
sivagnanam chandrakanth earned 500 total points
Comment Utility
See below code

<?php

$filesarray = array();

if ($handle = opendir('.')) {
    while (false !== ($entry = readdir($handle))) {
        if ($entry != "." && $entry != ".." && is_file($entry)) {
            //echo "$entry<br />";
            $filesarray[] = $entry;
        }
    }
    closedir($handle);
}
foreach($filesarray as $key=>$value){
echo $value."<br>";
}
//print_r($filesarray);
?>

Open in new window

0
 

Author Closing Comment

by:rgb192
Comment Utility
$key=>value works
thank you
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now