如何打印出两个关联数组并使用循环来爆炸第二个数组值? 问题是爆炸
数组属性将限制为3个值,值将限制为4个。
[attributes] => Array ( [0] => Array ( [attribute] => Colour ) [1] => Array ( [attribute] => Size ) )
[values] => Array ( [0] => Array ( [value] => Red,Green,Blue ) [1] => Array ( [value] => Large,Medium,Small ) )
如果有帮助我可以将值的密钥保存为属性名称:
Array ( [colour] => Red,Green,Blue )
代码:
foreach ($attributes as $k => $v)
{
echo "<b>" .$v['attribute'] ."</b>"."<br>";
foreach ($values as $val)
{
$value = $val['value'];
$expld = explode(",", $value);
foreach ($expld as $explval)
{
$qryString = array( 'search' => $search,
'attr' => $explval
);
echo anchor('products/item_search?'. http_build_query($qryString), $explval) ."<br>";
}
}
}
答案 0 :(得分:1)
for ($i = 0; $i<count($attributes); $i++)
{
echo "<b>" .$attributes[$i]['attribute'] ."</b>"."<br>";
$expld = explode(",", $values[$i]['value']);
foreach ($expld as $explval)
{
$qryString = array( 'search' => $search,
'attr' => $explval
);
echo anchor('products/item_search?'. http_build_query($qryString),$explval) ."<br>";
}
}
答案 1 :(得分:1)
你不想遍历内部循环中的所有$values
,你只想爆炸与当前属性具有相同索引的那个并循环遍历那些。
foreach ($attributes as $index => $v) {
echo "<b>{$v['attribute']}</b><br>";
foreach (explode(',', $values[$index]['value']) as $explval) {
$qryString = array( 'search' => $search,
'attr' => $explval
);
echo anchor('products/item_search?'. http_build_query($qryString), $explval) ."<br>";
}
}