1

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

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

1 Answer 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']
  );
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, that is exactly what I was looking for. In my head this was far more complicated for some reason.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.