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
96
41
Other php-forum
Hotlinking Picasa as the image folder of a website
Hi there PHP freaks, I would like to create a private album in Picasa to use it as the image folder
How do i use vars in an array
Hiya peeps!
How do I use a var in an array. You will see $id but it isnt working.
ereg_replace issue
hi there people
i have this code happening with regards to my wamp server. is this something
php mysql query from input textbox
Hi,
I have a text box, in that i have given the mysql query. I can able to get the query in v
array & querys help/advice.
Ok so here goes , I have a mysql database and basically here's what I need to do.
// foreach
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
Problem with php's rename function
So, i'm having a problem with the php rename function. Basically my script 'delete_user.php' attempt
update sql when refresh - php
hi
I have made a table (attachement)
the users can update the sql database using + or x bu
Form help: Syntax & Logic
Hello again all,
I'm working on a form and ran into a wall (again) and can't seem to think th
error checking breaking my code
Hi there, OK first of all, big apologies for what I assume is really fundamental errors in the struc