Page 1 of 1

Loop problem

Posted: Sat Sep 06, 2008 7:15 pm
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]);

}

Re: Loop problem

Posted: Sun Sep 07, 2008 9:59 am
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.

Re: Loop problem

Posted: Sun Sep 07, 2008 11:03 am
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.

Re: Loop problem

Posted: Sun Sep 07, 2008 11:05 am
by starram
Use foreach function

here is the example

foreach ($arr as $value)
{
echo "Value: " . $value . "<br />";
}

Re: Loop problem

Posted: Sun Sep 07, 2008 12:00 pm
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 />';
    }
}