foreach code in PHP
I would like to iterate through an xml document to get its values. See the given code
foreach ($xml->children() as $key1=>$value1 /*($xm开发者_Python百科l->children() as $second_gen)*/ ) {
echo ' 1 ' .$key1.' '.$value1.'<br>';
foreach ($second_gen as $key2=>$value2) {
echo ' ___2 ' .$key2.' '.$value2.'<br>';
}
}
So what I want to do is to make $second_gen
equals to the children of the each current iteration of the loop. I was able to do this by putting it in the foreach, but this prevented me from using key/value. So is there any solution to get both?
Thanks!
The value in a foreach loop equals to the value itself. So if you leave out the $key =>
part or not doesn't change the $value
:
foreach ( $xml->children() as $key1 => $value1 )
{
foreach ( $value1->children() as $key2 = $value2 )
{
// ...
}
}
精彩评论