PHP-Array

<Beginner PHP><Array><Loop><Function><Pre-Def variable><Session><Read Write File><CRUD>

Numeric Arrays

Associate numeric index with their value.
Index starts at 0 (zero)
Index can be assigned automatically

Exemple 

Ex - Automatic assignement
$names=array("David","Amy","John");
Ex - Manual assignement
$names[0]="David";
$names[1]="Amy";
$names[2]="John";

Accessing the array element

Accessing through its indice
echo $names[1];//outputs "Amy"

Array and data types

In one array you can have many data types
 <?php
$ma[0]="David";
$ma[1]="<strong>PHP</strong>";
$ma[2]=21;
echo "$ma[0] is $ma[2] and knows$ma[1]";
?>

Associative Arrays

It uses named keys that we assign to them
can be done in two ways

$people = array("David"=>"27","Amy"=>"21"."John"=>"42");

or

$people[David]="27";
$people[Amy]="21";
$people[John]="42";

Note


"=>" is used here

Access array`s member

echo $people[`Amy`]; // outputs 21

Multi dimensional arrays

It contains one or more arrays dimension of array = number of indice needed

Two dimensional array

$people=array(
`online`=>array(`David`,`Amy`),
`offline`=>array(`John`,`Rob`,`Jack`),
`away`=>array(`Arthur`,`Daniel`)
);

echo $people[`online`][0]; // outputs `David`
echo $people[`away`][1]; // outputs `Daniel`


Comments

Popular posts from this blog

Programming- AI, machine learning

Programming -- JS, C, C++, Python

links - Robotic