我需要这个数组的帮助,我想做的是
我无法循环到第二级,但我不知道如何为第三级做,我认为它很容易,但我能搞清楚。
这是指向我页面的链接,因为你可以看到我通过第一和第二级别我想要回溯到第三级 http://communita.com.mx/marca_producto.php?id=6
希望你能帮助我。 提前谢谢。$qry = "
SELECT nomPadre,pp.idPadre, pathImgPadre,c.idCategoria,nomCategoria
FROM productos p
LEFT JOIN productos_categoria pc ON pc.idProducto = p.idProducto
LEFT JOIN productos_padre pp ON pp.idProducto = p.idProducto
LEFT JOIN categorias c ON c.idCategoria = pc.idCategoria
LEFT JOIN padre pa ON pa.idPadre = pp.idPadre
LEFT JOIN imagenesPadre i ON i.idPadrePa = pp.idPadre
WHERE marProducto = :idMarca AND i.idMarcaPa = :idMarca AND c.catPadre = pp.idPadre";
$stmt = $db->prepare($qry);
$stmt->execute(array(':idMarca' => $_GET['id']));
while ($row=$stmt->fetch()) {
// usa el idPadre como key para agrupar el arreglo
// these two values will just overwrite with the same thing if they're repeated
$padres[$row['idPadre']]['nomPadre'] = $row['nomPadre'];
$padres[$row['idPadre']]['idPadre'] = $row['idPadre'];
$padres[$row['idPadre']]['idMarca'] = $row['idMarca'];
$padres[$row['idPadre']]['pathImgPadre'] = $row['pathImgPadre'];
$padres[$row['idPadre']]['categoria'][$row['idCategoria']] = $row['nomCategoria'];
}
这就是我如何回复信息
<ul class="grid cs-style-4 col-xs-12 col-sm-12 col-md-12 text-center">
<?php foreach ($padres as $padre): ?>
<li class="col-md-3 col-lg-3">
<figure>
<div class="">
<img src="<?= $padre['pathImgPadre']?>" alt="<?= $padre['nomPadre']?>">
</div>
<figcaption class="subcate">
<?php foreach ($padre['categoria'] as $id => $categoria): ?>
<a href="cateXmarca.php?ma=<?= $_GET['id']?>&pa=<?= $padre['idPadre']?>&ca=<?=$id?>"><h6>• <?= $categoria ?></h6></a><br>
<?php endforeach ?>
</figcaption>
</figure>
<div class="col-xs-12 col-sm-12 col-md-12 text-center">
<span class="caption"><?= $padre['nomPadre']?></span>
</div>
</li>
<?php endforeach ?>
</ul>
答案 0 :(得分:0)
要通过for
循环访问数组深度,您可以这样做。 if
语句用于确保在数组的一部分不具有相同深度的情况下不存在错误。查看在线DEMO here
<?php
$arr = array(
"ILUMINATION" => array(
"LAMPS" => array(
"desk",
"wall"
)
),
"Furniture" => array(
"chairs",
"tables" => array(
"entertaiment"
)
)
);
foreach($arr as $catagory){
//first layer, will output: ILUMINATION, Furniture
if(is_array($catagory)){
foreach($catagory as $sub_catagory){
//second layer, will ouput: LAMPS, chairs, tables
if(is_array($sub_catagory)){
foreach($sub_catagory as $item){
//third layer, will output: desk, wall, entertaiment
}
}
}
}
}
?>
要通过简单的选择来访问它,请查看此示例:或者查看DEMO here
<?php
$arr = array(
"ILUMINATION" => array(
"LAMPS" => array(
"desk",
"wall"
)
),
"Furniture" => array(
"chairs",
"tables" => array(
"entertaiment"
)
)
);
echo $arr["Furniture"]["tables"][0];
?>
这将为您提供娱乐。您需要记住使用0
添加额外的深度,因为它仍然是您尝试访问的数组项。
要添加到多维arrays
,请在此处按照此示例操作:
$arr["Furniture"]["sofas"] = array("leather", "fabric");