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
Dave2000
Forum Contributor
Posts: 126
Joined: Wed Jun 21, 2006 1:48 pm

arrays

Post by Dave2000 »

Code: Select all

$bakery = array(
"cherry" => array("£100","2 pieces","sugar"),
"apple" => array("£200","3 pieces","sugar"),
"other" => array("£300","1 piece","sugar-free"),
);
The number "300" could be called by the index, $bakery[other][0]

However, is it possible, and if so how...

do i assign a key to the 300 index, so it could be called by something like $bakery[other][cost]

I hope this makes sense.

Shears :)
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

Exactly like you did it with "cherry", "apple" and "other".
Also, get rid of the comma after the last array :wink:
Dave2000
Forum Contributor
Posts: 126
Joined: Wed Jun 21, 2006 1:48 pm

Post by Dave2000 »

Thank you. I realised what i did wrong. :oops:

I have another question. It's different but related to the same array so i'll ask here...

Code: Select all

'5' => array('gold' => '50,000,000 gold','Uprade to Level 5','cost' => '50000000')
The seperator/commas in "50,000,000 gold" seem to be confusing the associative array. I tried escaping with a backslash before the commas but that didn't seem to make an difference. Is it possible to leave the commas in?

Thank you :)

Shears
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

They don't confuse the array. :?
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

The commas shouldn't cause any problems, it's probably something else you did wrong.
Dave2000
Forum Contributor
Posts: 126
Joined: Wed Jun 21, 2006 1:48 pm

Post by Dave2000 »

Ahh i see what i did wrong. I did not realise that all the keys get shifted down one if there is an "associative style" name of an index before indexes with normal key naming - if that makes sense lol. I thought this affect was caused by the commas. Oh well. Thank you ^^

Shears :)
Post Reply