PHP arrays

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
webmonkey88
Forum Newbie
Posts: 20
Joined: Fri Aug 14, 2009 4:30 am

PHP arrays

Post by webmonkey88 »

Just come across something that is a little weird, just noticed that you can do the following with arrays:

Code: Select all

 
$myarray = array(1 => 1, 2=> 2, 3 => 3, 4 => 4);
$myarray{5} = 5;
 
//array now has key 5 value 5 as if i had done $myarray[5] = 5
 


why can you use {} as well as [], but if i use it like the following it will throw a syntax error:

Code: Select all

 
$myarray{} = 5;
 
Anyone know where i can find some information on this i have googled but cant find anything.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: PHP arrays

Post by jackpf »

I read this in the manual a while ago...can't remember which page.

But yeah, you can use curly or square parenthesis for array keys.
wpsd2006
Forum Commoner
Posts: 66
Joined: Wed Jan 07, 2009 12:43 am

Re: PHP arrays

Post by wpsd2006 »

wow that's new

that might be easier then

But I think we shouldn't use it after we find a real documentation with this cuz let say it's a bug and get deprecated......
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: PHP arrays

Post by jackpf »

Well it's not a bug. I'm sure they realise this happens.

Although i think the method of using them to access a character in a string is deprecated...so people don't use it as much as square brackets.

i could be wrong though. Can't remember where that damn page is...
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: PHP arrays

Post by jackpf »

Oh cool, I was right :)
Post Reply