通过查询创建多维数组 [英] Creating a multi-dimensional array from query
本文介绍了通过查询创建多维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用下面的代码从查询中创建多维数组,以便我可以按类别组织结果,但是它只给我2列(类别,代理商)。
I am using the code below to create a multi-dimensional array from a query so that I can organize the results by category but it only gets me 2 columns (category, agency).
我不确定如何更改此设置,以便获得4列(类别,代理商,说明,网站)。
I am not sure how I can alter this so that I can get 4 columns (category, agency, description, website). Any help is greatly appreciated.
$categories = array();
while ($row = mysqli_fetch_array($result))
{
$category = $row['category'];
$categories[$category][] = $row['agency'];
}
<?php
foreach ($categories as $category => $agencies)
{
?>
<h3><?php echo $category; ?></h3>
<table class="chart">
<?php
foreach ($agencies as $agency)
{
?>
<tr><td><?php echo $agency; ?></td></tr>
<?php
}
?>
</table>
<?php
}
?>
推荐答案
您可以将单个行的结果存储为关联数组:
You could store the individual row results as an associative array:
$categories[$category][] = array(
'agency' => $row['agency'],
'description' => $row['description'],
'website' => $row['website']
);
这篇关于通过查询创建多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文