从查询创建多维数组

时间:2021-10-23 21:30:53

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).

我使用下面的代码从查询创建一个多维数组,以便我可以按类别组织结果,但它只给我2列(类别,代理)。

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.

我不知道如何改变这一点,以便我可以获得4列(类别,代理,描述,网站)。任何帮助是极大的赞赏。

$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
    }
?>

1 个解决方案

#1


4  

You could store the individual row results as an associative array:

您可以将各个行结果存储为关联数组:

$categories[$category][] = array(
      'agency' => $row['agency'], 
      'description' => $row['description'], 
      'website' => $row['website']
  );

#1


4  

You could store the individual row results as an associative array:

您可以将各个行结果存储为关联数组:

$categories[$category][] = array(
      'agency' => $row['agency'], 
      'description' => $row['description'], 
      'website' => $row['website']
  );