FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » Dot in array key name
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
Dot in array key name [message #169966] Sat, 02 October 2010 06:30 Go to next message
Martin is currently offline  Martin
Messages: 8
Registered: October 2010
Karma: 0
Junior Member
Hi.

I'm debugging a script where i load images if they haven't already
been loaded and then draw them onto another image.
(All images are PNGs).

$icons=array();

$icon_name='myicon.png';

if($icons[$icon_name]){
// the image has already been loaded
$icon=$icons[$icon_name];
} else {
$icon=imagecreatefrompng($icon_name);
$icons[$icon_name]=$icon;
}

I'm trying not to load an image more than once if it is required more
than once.

Anyway - i have a bug where sometimes the wrong image is used and
wondered whether the dot in the image's filename is invalid syntax for
an array key name?

I've searched a while but get lots of irrelevant results - if anyone
can give me a quick yes or no answer it'd be appreciated.

Martin.
Re: Dot in array key name [message #169971 is a reply to message #169966] Sat, 02 October 2010 13:19 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 10/2/2010 2:30 AM, Martin wrote:
> Hi.
>
> I'm debugging a script where i load images if they haven't already
> been loaded and then draw them onto another image.
> (All images are PNGs).
>
> $icons=array();
>
> $icon_name='myicon.png';
>
> if($icons[$icon_name]){
> // the image has already been loaded
> $icon=$icons[$icon_name];
> } else {
> $icon=imagecreatefrompng($icon_name);
> $icons[$icon_name]=$icon;
> }
>
> I'm trying not to load an image more than once if it is required more
> than once.
>
> Anyway - i have a bug where sometimes the wrong image is used and
> wondered whether the dot in the image's filename is invalid syntax for
> an array key name?
>
> I've searched a while but get lots of irrelevant results - if anyone
> can give me a quick yes or no answer it'd be appreciated.
>
> Martin.

No, there is not a problem with a period in an array key. It's just
another character.

Your problem lies somewhere else.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: Dot in array key name [message #169973 is a reply to message #169966] Sat, 02 October 2010 13:39 Go to previous messageGo to next message
August Karlstrom is currently offline  August Karlstrom
Messages: 16
Registered: October 2010
Karma: 0
Junior Member
On 2010-10-02 08:30, Martin wrote:
> $icons=array();
>
> $icon_name='myicon.png';
>
> if($icons[$icon_name]){
> // the image has already been loaded
> $icon=$icons[$icon_name];
> } else {
> $icon=imagecreatefrompng($icon_name);
> $icons[$icon_name]=$icon;
> }

In the code above you reset the icon array each time the script is run,
which is probably not what you want. You should also use the function
isset in the if-statement condition so it can handle all file names
correctly including "0" and "0.0" (both interpreted as boolean false):

if (!isset($icons[$icon_name])) {
$icons[$icon_name] = imagecreatefrompng($icon_name);
}
$icon = $icons[$icon_name];

> I'm trying not to load an image more than once if it is required more
> than once.
>
> Anyway - i have a bug where sometimes the wrong image is used and
> wondered whether the dot in the image's filename is invalid syntax for
> an array key name?

No, it's perfectly valid.


/August

--
The competent programmer is fully aware of the limited size of his own
skull. He therefore approaches his task with full humility, and avoids
clever tricks like the plague. --Edsger Dijkstra
Re: Dot in array key name [message #169985 is a reply to message #169973] Sun, 03 October 2010 02:04 Go to previous messageGo to next message
Martin is currently offline  Martin
Messages: 8
Registered: October 2010
Karma: 0
Junior Member
Thanks for the replies.

The code i posted was typed from memory and being a javascript bod i
didn't get the syntax correct, but in the code i'm debugging i did use
isset() and the array wasn't being reset within the loop.

I had previously done a simple test which produced no errors:

<?php

$i=array();

$i['myName']='this is ok';

$i['afile.nam']='is this ok?';

print_r($i);

?>

So was hoping that the dot was ok which is now confirmed.

I shall look elsewhere for the bug....

Martin.


August Karlstrom wrote:
> On 2010-10-02 08:30, Martin wrote:
>> $icons=array();
>>
>> $icon_name='myicon.png';
>>
>> if($icons[$icon_name]){
>> // the image has already been loaded
>> $icon=$icons[$icon_name];
>> } else {
>> $icon=imagecreatefrompng($icon_name);
>> $icons[$icon_name]=$icon;
>> }
>
> In the code above you reset the icon array each time the script is run,
> which is probably not what you want. You should also use the function
> isset in the if-statement condition so it can handle all file names
> correctly including "0" and "0.0" (both interpreted as boolean false):
>
> if (!isset($icons[$icon_name])) {
> $icons[$icon_name] = imagecreatefrompng($icon_name);
> }
> $icon = $icons[$icon_name];
>
>> I'm trying not to load an image more than once if it is required more
>> than once.
>>
>> Anyway - i have a bug where sometimes the wrong image is used and
>> wondered whether the dot in the image's filename is invalid syntax for
>> an array key name?
>
> No, it's perfectly valid.
>
>
> /August
>
> --
> The competent programmer is fully aware of the limited size of his own
> skull. He therefore approaches his task with full humility, and avoids
> clever tricks like the plague. --Edsger Dijkstra
Re: Dot in array key name [message #169993 is a reply to message #169985] Sun, 03 October 2010 19:20 Go to previous messageGo to next message
Tim Roberts is currently offline  Tim Roberts
Messages: 2
Registered: September 2010
Karma: 0
Junior Member
Martin <warwound(at)gmail(dot)com> wrote:
>
> I had previously done a simple test which produced no errors:
>
> <?php
> $i=array();
> $i['myName']='this is ok';
> $i['afile.nam']='is this ok?';
> print_r($i);
> ?>
>
> So was hoping that the dot was ok which is now confirmed.
>
> I shall look elsewhere for the bug....

My first wild guess would be capitalization. myName.png and myname.png are
two different entries, as far as the array is concerned.
--
Tim Roberts, timr(at)probo(dot)com
Providenza & Boekelheide, Inc.
Re: Dot in array key name [message #169998 is a reply to message #169985] Mon, 04 October 2010 09:50 Go to previous message
Captain Paralytic is currently offline  Captain Paralytic
Messages: 204
Registered: September 2010
Karma: 0
Senior Member
On 3 Oct, 03:04, Martin <warwo...@gmail.com> wrote:
> August Karlstrom wrote:
>> On 2010-10-02 08:30, Martin wrote:
>>> $icons=array();
>
>>> $icon_name='myicon.png';
>
>>> if($icons[$icon_name]){
>>>   // the image has already been loaded
>>>   $icon=$icons[$icon_name];
>>> } else {
>>>   $icon=imagecreatefrompng($icon_name);
>>>   $icons[$icon_name]=$icon;
>>> }
>
>> In the code above you reset the icon array each time the script is run,
>> which is probably not what you want. You should also use the function
>> isset in the if-statement condition so it can handle all file names
>> correctly including "0" and "0.0" (both interpreted as boolean false):
>
>> if (!isset($icons[$icon_name])) {
>>    $icons[$icon_name] = imagecreatefrompng($icon_name);
>> }
>> $icon = $icons[$icon_name];
>
>>> I'm trying not to load an image more than once if it is required more
>>> than once.
>
>>> Anyway - i have a bug where sometimes the wrong image is used and
>>> wondered whether the dot in the image's filename is invalid syntax for
>>> an array key name?
>
>> No, it's perfectly valid.
>
>> /August
>
>> --
>> The competent programmer is fully aware of the limited size of his own
>> skull. He therefore approaches his task with full humility, and avoids
>> clever tricks like the plague. --Edsger Dijkstra

Please do not top post (top posting fixed)
> Thanks for the replies.
>
> The code i posted was typed from memory and being a javascript bod i
> didn't get the syntax correct, but in the code i'm debugging i did use
> isset() and the array wasn't being reset within the loop.

It is a lot easier for us to help you with problems in your code if
you post the code that has problems rather than other arbitrary code
that you have made up on teh spot.
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Inserting into associative array
Next Topic: php ide with checkpoint restart is there?
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Sun Nov 10 16:03:36 GMT 2024

Total time taken to generate the page: 0.02683 seconds