Visit the Official PHP Website

Search this Site

PHP Programming

By James N Hitz

Adding items to an array

Adding an item to an array is still a simple affair. If you are adding to an exisisting 'room', you use the array's name and the 'room' number in square brackets. For example to replace the contents of 'room' 0 we code...

$fruits[0] = "apples";

Revolution! apples have ousted oranges out of room 0. Poor oranges, what a nightmare! Our array now looks like this:

012
applesmangoespawpaws

If you are the more peaceful type and you don't fancy ousting others out of their rooms, you could still assign a value to the "next available room" like so...

$fruits[] = "bananas";

bananas has been added at the next available room, and our array should now look something like this:

0123
orangesmangoespawpawsbananas

See how peaceful bananas are? If only we were all as considerate!

That is all fine but... What if I don't like the 'room numbers'? What if I am more interested in 'suites'?

<< Creating a List Array | Associative Arrays >>

JamHitz Productions