如何在一个foreach中使用两个变量?

时间:2015-04-23 18:20:21

标签: php

我想用2种不同的分类法(var)生成Boostrap Acordion。但我不能在一个Foreach中结合两者。此外,我看这个论坛,我尝试了一些anwers,但我只是得到错误。

<?php

$temporada = 'temporada';
$capitulo = 'capitulo';
$tax_temporada = get_terms($temporada);
$tax_capitulo = get_terms($capitulo);

foreach ($tax_temporada as $tax_temporada) {
echo '<div class="panel panel-default">';
echo '<div class="panel-heading" role="tab" id="c'. $tax_capitulo->name.'">';
echo '<h4 class="panel-title">' . '<a data-toggle="collapse" data-parent="#accordion" href="#'. $tax_temporada->name.'" aria-expanded="true" aria-controls="' . $tax_temporada->name.'" >Temporada: ' . $tax_temporada->name.'</a></h4>';
echo '</div>';
echo '<div id="'. $tax_temporada->name.'" class="panel-collapse collapse" role="tabpanel" aria-labelledby="c'. $tax_capitulo->name.'">';
echo '<div class="panel-body">';
echo 'Capitulo '. $tax_capitulo->name.' ';
echo '</div>'.'</div>'.'</div>';

}
?> 

问题是我能得到一个Var而不是两个

这一个在这里修复了问题,但事情是我不能在每个$ temporada中超过1 $ capitulo

<?php

$temporada = 'temporada';
$capitulo = 'capitulo';
$tax_temporada = get_terms($temporada);
$tax_capitulo = get_terms($capitulo);

for ($i = 0; $i < count($tax_temporada); $i++) {
echo '<div class="panel panel-default">';
echo '<div class="panel-heading" role="tab" id="c'. $tax_capitulo[$i]->name.'">';
echo '<h4 class="panel-title">' . '<a data-toggle="collapse" data-parent="#accordion" href="#'. $tax_temporada[$i]->name.'" aria-expanded="true" aria-controls="' . $tax_temporada[$i]->name.'" >Temporada: ' . $tax_temporada[$i]->name.'</a></h4>';
echo '</div>';
 //Here Start the $capitulos that go inside of every $temporadas 
echo '<div id="'. $tax_temporada[$i]->name.'" class="panel-collapse collapse" role="tabpanel" aria-labelledby="c'. $tax_capitulo[$i]->name.'">';
echo '<div class="panel-body">';
echo 'Capitulo '. $tax_capitulo[$i]->name.' ';
echo '</div>'.'</div>'.'</div>';

}
?>

2 个答案:

答案 0 :(得分:3)

这两个都是相同长度的对象数组吗?如果是这样,你可以考虑做一个简单的for循环,如下:

<?php

$temporada = 'temporada';
$capitulo = 'capitulo';
$tax_temporada = get_terms($temporada);
$tax_capitulo = get_terms($capitulo);

for ($i = 0; $i < count($tax_temporada); $i++) {
    echo '<div class="panel panel-default">';
    echo '<div class="panel-heading" role="tab" id="c'. $tax_capitulo[$i]->name.'">';
    echo '<h4 class="panel-title">' . '<a data-toggle="collapse" data-parent="#accordion" href="#'. $tax_temporada[$i]->name.'" aria-expanded="true" aria-controls="' . $tax_temporada[$i]->name.'" >Temporada: ' . $tax_temporada[$i]->name.'</a></h4>';
    echo '</div>';
    echo '<div id="'. $tax_temporada[$i]->name.'" class="panel-collapse collapse" role="tabpanel" aria-labelledby="c'. $tax_capitulo[$i]->name.'">';
    echo '<div class="panel-body">';
    echo 'Capitulo '. $tax_capitulo[$i]->name.' ';
    echo '</div>'.'</div>'.'</div>';

}
?> 

答案 1 :(得分:0)

MultipleIterator允许您同时循环多个数组

$tax_temporada = get_terms($temporada);
$tax_capitulo = get_terms($capitulo);

$taxIterator = new MultipleIterator();
$taxIterator->attachIterator(new ArrayIterator($tax_temporada));
$taxIterator->attachIterator(new ArrayIterator($tax_capitulo));
foreach($taxIterator as list($temporada, $capitulo)) {
   ... do what you want here with $temporada and $capitulo
}

修改

请注意,使用list()解压缩嵌套数组需要PHP&gt; = 5.5.0