Re: adding key-> value pair to an array
| From: | Jim Lucas | Date: | Wed, 03 Dec 2008 17:04:39 +0000 |
| Subject: | Re: adding key-> value pair to an array | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-284061@lists.php.net to get a copy of this message | ||
Yeti wrote:
>> //for each row I want to add percentage as new key->value pair
>> // but it gives error 'Undefined variable:
>> percentage'
>> $row->$percentage = ($browseCount / $totalCount ) * 100;
>
> Obviously you get the error because $percentage is not defined ..
>
> I did not fully understand what you wanted.
>
> Here is a list of what should work ...
>
> #Set a variable percentage in $row
> $row->percentage = ($browseCount / $totalCount ) * 100;
>
> #Set a variable with name $percentage in $row
> $percentage = ($browseCount / $totalCount ) * 100;
> $row->$percentage = $percentage;
>
I don't think the above will work either, afaik php doesn't allow you to have variable
names start with numbers. Also, there is a high possibility
that $percentage would end up being a float.
--
Jim Lucas
"Some men are born to greatness, some achieve greatness,
and some have greatness thrust upon them."
Twelfth Night, Act II, Scene V
by William Shakespeare