Jump to content

Renaming array keys


soycharliente

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/188016-renaming-array-keys/
Share on other sites

You'll need to work directly on the array:

 

foreach ($info as $key => $value)
{
    // Unset the old key/value pair
    unset($info[$key]);
    // Make new key
    $key = $key . (number_format($value/$total,0)) . '%';
    // Add in the new key/value pair
    $info[$key] = $value;
}

 

You could also just build an array of new keys and use that when constructing the URL:

 

foreach ($info as $key => $value) {
    $percent_keys[] = $key . (number_format($value/$total,0)) . '%';
}
// ...
echo implode('|', $percent_keys); // in place of "echo implode('|',array_keys($info));"

Link to comment
https://forums.phpfreaks.com/topic/188016-renaming-array-keys/#findComment-992717
Share on other sites

I used the first solution. Thanks.

 

Also, just in case it helps anyone, I found an error no one caught in my code! When calculating a percentage, I might want to multiply the formula by 100 if I'm going to give it in terms of number% ;)

 

foreach ($info as $key => $value)
{
unset($info[$key]); // Unset the old key/value pair
$key = "{$key} " . (number_format($value/$total*100,1)) . '%'; // Make new key
$info[$key] = $value; // Add in the new key/value pair
}

Link to comment
https://forums.phpfreaks.com/topic/188016-renaming-array-keys/#findComment-992866
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.