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]);
}
Loop problem
Moderator: General Moderators
Re: Loop problem
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
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
Use foreach function
here is the example
foreach ($arr as $value)
{
echo "Value: " . $value . "<br />";
}
here is the example
foreach ($arr as $value)
{
echo "Value: " . $value . "<br />";
}
Re: Loop problem
Code: Select all
for ($i = 0; $i < $mast; ++$i) {
foreach($docroot[$i] as $key=>$value) // or '$docroot[$i] as $value'
{
// do your thing here
}
}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 />';
}
}