Loop problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
aliahsan81
Forum Newbie
Posts: 8
Joined: Thu Sep 04, 2008 4:46 pm

Loop problem

Post by aliahsan81 »

Hi All

I have any array dynamic which contain values string,I want to get each value from array one by one outside the loop how can i do this.

<?php

$docroot=shell_exec('grep ^DocumentRoot /etc/httpd/conf.d/*.conf|cut -d" " -f2| uniq');
$docroot=str_replace('"', "", $docroot);
$docroot = explode("\n",$docroot);
print_r ($docroot);
$mast=count($docroot);
$mast=$mast-1;
for ($i = 0; $i < $mast; ++$i) {


print_r ($docroot[$i]);

}
koen.h
Forum Contributor
Posts: 268
Joined: Sat May 03, 2008 8:43 am

Re: Loop problem

Post by koen.h »

You could have an loop inside the loop to iterate over the values. Or create a function that handles that. I'm not sure why it has to be outside the loop.
aliahsan81
Forum Newbie
Posts: 8
Joined: Thu Sep 04, 2008 4:46 pm

Re: Loop problem

Post by aliahsan81 »

i want to get the value of array one by one to pass below to do some calculation.I am not able to write nested loop plz can u guid me or prepare me a nested loop i shell be very thank full to you.
User avatar
starram
Forum Commoner
Posts: 58
Joined: Thu Apr 10, 2008 1:27 am
Location: India
Contact:

Re: Loop problem

Post by starram »

Use foreach function

here is the example

foreach ($arr as $value)
{
echo "Value: " . $value . "<br />";
}
koen.h
Forum Contributor
Posts: 268
Joined: Sat May 03, 2008 8:43 am

Re: Loop problem

Post by koen.h »

Code: Select all

 
for ($i = 0; $i < $mast; ++$i) {
    foreach($docroot[$i] as $key=>$value) // or '$docroot[$i] as $value'
        {
             // do your thing here
        }
}
take this as an example of a loop in a loop:

Code: Select all

for ($i=0; $i<5; ++$i)
{
    for ($j=0; $j<5; ++$j)
    {
        echo '$i is now: ' . $i . ' and $j is now: ' . $j;
        echo '<br />';
    }
}
Post Reply