Renaming array keys
Posted on
16th Feb 2014 07:03 pm by
admin
The issue: renaming array keys inside a foreach loop.
I'm trying to build a pie chart image with the Google Chart API using information from a database. (I'm going to change the type of data I talk about because it is extremely sensitive. Just wanted to share that so you didn't think my example was completely weird or stupid even.)
After running my SELECT statement, I have a 2-column dataset with multiple rows. I am currently grabbing the data in this way:
<?php
$total = 0;
while ($r = mysql_fetch_array($result))
{
$info[$r['month']] = $r['days'];
$total += $r['days'];
}
?>
$info is an array with each key being the name of the month and the value being how many days are in that month. $total is the sum of each days.
To supply the dataset inside the image src, I provide a comma separated list. To supply the labels for each dataset, I provide a list separated by pipes.
The problem that I am having is that I would like each label to be the month as well as a percentage number of the total in parenthesis next to the month name.
I am building the URL perfectly fine using JUST the month name like this:
<?php
echo '<p>';
echo '<img src="http://chart.apis.google.com/chart?cht=p&chd=t:';
echo implode(',',array_values($info));
echo '&chs=250x100&chl=';
echo implode('|',array_keys($info));
echo '" alt="" />';
echo '</p>';
?>(I broke up each line for readability.)
How I am trying to include the percentage in the label is be redefining the key BEFORE I add in the array keys to the URL. Trying by this method:
<?php
foreach ($info as $key => $value)
{
$key = $key . (number_format($value/$total,0)) . '%';
}
?>
Expected outcome:
The labels on the image would read something to the tune of -- June (8.2%)
Actual outcome:
Nothing. Well, not nothing. Just the month is coming out. The key names are not being edited.
Thanks for reading this far. I appreciate the help
No comments posted yet
Your Answer:
Login to answer
96
41
Other forums
need Array help
This is what I have to do.
$teamname[1] = "Red Sox"
$teamname[2] = "Gian
list files from folder, only one for each date
I have many files in a folder and wish to only list one of each date
these are the file names
ORA-01655: unable to extend cluster SYS.C_FILE#_BLOCK# by 128
Hi all,
Oracle 10g. Linux.
I'm facing the below error while trying to import a dumpfil
Error in write byte array into document in SM58
Hi Gurus,
Getting the following error status in Transactional RFC (SM58) ..... *Error i
creating a 1 to 100 in a table
hi guys I am a newb in php need some help.
I have a table with 1 column and 100 rows and in
Deleted Delivery but material wont return
We deleted a delivery but the material its still pending.
We made a reverse goods movemen
ereg_replace in Wordpress
Heya - so I'm working on this site: http://world-of-smiles.theportlandco.com/new-patients
The
Running External Scripts
I am very new to PHP and am not even sure what I'm asking is possible. I have just installed mediaW
my sql select id and then update problem.
I need to select some auto incremented ids out of a database and then use those id in a where statem
How can you detect variable string in a massive string?
Ok guys, i need some help with this one.
I am going to be getting a large string, like 10-20