Home | Help Forums | Web Design Tutorials | Free Swishmax Downloads | All Downloads

 

 

Company

Home

Nework Details

Tutorials

SWiSH 2

SWiSHMax

Flash MX

PHP & MySQL

HTML / JScript

Adobe Photoshop

Corel Draw

Gimp

Miscellaneous

Downloads

Templates

Plugins

Fonts

Wallpapers

Free Images

Scripts and Codes

Products

E-Books

SWiSH BB

SWiSH Templates

SWiSH-DB Newsletter
Subscribe to our newsletter : GO
  Partners / Affiliates

   Swish Templates
   SWiSH Climax
   GimpTalk
   Idea Designs
   Sposatoettore
   Try Acai Berry Diet
   Download Free Ringtones
   Swishzone

Manage your site with Flax Article Content Management System
Array in php
Category : PHP / MySQL | Level : Beginner | Language : English
Ask Question | Read Comments | Post tutorial | Previous | Next

Web www.swish-db.com

Hi all,
Assume you know what is a variable. If not see this ali tut here

As php is based on perl, this article is largely based on the array section of perl tutorial here.
It's an introduction to a next article on predefined variables in php.

-----------------
Array in php are like complex variables. They permit to store more than one variable in one variable. Think of them like a librairy(array) where you store books (variables)

there is many way to declare a variables as an array. Start with the more explicit one.

CODE
$COLORS = array('BLUE', 'YELLOW', 12, 45); // notice that an array could contain different type of variables

To access an entry, you need to know its index. By default, when you declare an array, php start from 0 and increment it.
When you know your index, simply call it with $array[index];
So if you understand well, to retrieve 'YELLOW', i will do
CODE
echo $COLORS[1];


------------------
Second way of declaring an array
CODE
$COLORS= array( 5 => 'BLUE', 6 => 'YELLOW', 'textIndex' => 'Great');

This way, you can define your index. And you see that index, could also be string.
In fact, you could use anything that is an expression even a variable. Look at this example

CODE
$myIndex= 'textIndex';
echo $COLORS[$myIndex]; // will print Great


-------------------
Third way, the most easier.
just do
CODE
$COLORS['textIndex']= 45;

COLORS will lost its previous contents if it wasn't an array, be converted to an array and will contain 45 in the textIndex placeholder.
this is the same as $COLORS=array('textIndex' => 45);

But what is more powerfull with this method, is that you can add item and replace other.
CODE
$COLORS['textIndex']= 45;
$COLORS['textIndex2']= 46;
$COLORS['textIndex']= 50;

this array will contain 50 and 46. when you use the array(...) method everything is deleted before adding new val.

Last but not least, when you add element this way, you could omit the index. PHP will use the current index of the array.
CODE
$COLORS= array('zero', 'one', 'two', 'three');
$COLORS[]= 'four';
echo $COLORS[4]; // will print four



Don't be afraid, i think you'll only have to know how to acess it and i will explain again latter in other tutorials.

-------------------
if you're lost, you could print an array variable (and any other type) with the print_r($variable).

regards
Sébastien,


All rights reserved - swish-db.com