简体   繁体   中英

Multi dimensional array from mysql for data visualization

First timer so please bear with me. Probably got this all wrong.

I am trying to return a multi dimensional array from the results of mysqli query.

$results= mysqli_query($con,"SELECT color, COUNT(*) FROM table WHERE type = 'post' AND author = '$user_ID'");
while($row=mysqli_fetch_array($results) {

I'd like to return something that ends up in this format.

$data = array(
  array(
    $row['color'],
    $row['COUNT(*)']
  ),
  array(
    $row['color'],
    $row['COUNT(*)']
  ),
  ....
  array(
    $row['color'],
    $row['COUNT(*)']
  );
);

This would then be used to populate a pie chart showing the color percentages. Any help would be appreciated.

When I create the arrays manually I get the desired results but I can not get any where trying dynamically.

$data = array(
  array(
    'red',
    20
  ),
  array(
    'blue',
    36
  ),
  ....
  array(
    green,
    10
  );
);

Try:

$rows_colors = array();

while($r = mysqli_fetch_assoc($results)) {

    $rows_colors[] = $r;
}

// Verify with:
print_r($rows_colors);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM