我目前有以下代码......它工作正常,但我需要更改它:
$group = array();
while ($row = mysqli_fetch_assoc($result)) {
$group[ $row['company'] ][] = $row;
}
foreach ($group as $company => $items) {
echo "$company<br/>";
foreach ($items as $item) {
$image = "item_img_url_code_goes_here";
echo "<img src='$image'/>";
$item_name = $item['item'];
$item_size = $item['item_size'];
$item_version = $item['item_version'];
echo "<p>$item_name - $item_size - $item_version</p>";
}
echo '<div class="clear"></div>';
}
这样的输出:
COMPANY 1
item - item size - item version
item - item size - item version
item - item size - item version
COMPANY 2
item - item size - item version
item - item size - item version
item - item size - item version
但我希望它输出如下:
COMPANY 1
item size one
item - item version
item - item version
item - item version
item size two
item - item version
item - item version
item - item version
etc
COMPANY 2
item size one
item - item version
item - item version
item - item version
item size two
item - item version
item - item version
item - item version
etc
有人能告诉我如何实现这一目标吗?
答案 0 :(得分:1)
您可以先将数据存储在一个具有如下结构的新数组中,而不是立即回显数据:
data['sizeA']['item1'] = item1
data['sizeA']['item2'] = item2
data['sizeB']['item1'] = item3
data['sizeB']['item2'] = item4
然后使用嵌套循环遍历该新数组:
foreach($data as $size -> $items) {
echo "$size";
foreach($items as $item) {
$name = $item['name'];
// etc.
echo "$name";
}
}