Example 1: set array (implicit numbering)
$ron1[]='A';
$ron1[]='B';
$ron1[]='C';

Result:

$ron1[0] = A
$ron1[1] = B
$ron1[2] = C

Example 2: (explicit numbering)
$ron2[0]='A';
$ron2[1]='B';
$ron2[2]='C';

Result:

$ron2[0] = A
$ron2[1] = B
$ron2[2] = C

Example 3: Range

$ron3=range(D,F);

Result:

$ron3[0] = D
$ron3[1] = E
$ron3[2] = F

Example 4: read array (foreach method)

$ron4[0]='A';
$ron4[1]='B';
$ron4[2]='C';

foreach ($ron4 as $key => $value){
echo "the value at $Ron4 Key $key is $value.<br />";
}

Result:

the value at $Ron4 Key 0 is A.
the value at $Ron4 Key 1 is B.
the value at $Ron4 Key 2 is C.

Example 5 Associative Array

$ron5 = array('A'=>1, 'B'=>2, 'C'=>3);

foreach ($ron5 as $key => $value){
echo "the value at $Ron5 Key $key is $value.<br />";

Result:

the value at $Ron5 Key A is 1.
the value at $Ron5 Key B is 2.
the value at $Ron5 Key C is 3.

 


Example 6 Associative Array

$ron6 = array('A'=>1);
$ron6['B']=2;
$ron6['C']=3;

foreach ($ron6 as $key => $value){
echo "the value at $Ron6 Key $key is $value.<br />";
}

Result:

the value at $Ron6 Key A is 1.
the value at $Ron6 Key B is 2.
the value at $Ron6 Key C is 3.

 


Example 7 Associative Array

$ron7['A']=1;
$ron7['B']=2;
$ron7['C']=3;

foreach ($ron7 as $key => $value){
echo "the value at $Ron7 Key $key is $value.<br />";
}

Result:

the value at $Ron7 Key A is 1.
the value at $Ron7 Key B is 2.
the value at $Ron7 Key C is 3.

 


Example 8 Associative Array While Example

$ron8['A']=1;
$ron8['B']=2;
$ron8['C']=3;

while ( $element = each ( $ron8 ))
{
echo 'element '.$element [ 'key' ];
echo ' has value of ' ];
echo $element [ 'value' ];
echo '<br />;
}

Result:

element A has value of 1
element B has value of 2
element C has value of 3

Example 9 Associative Array List Example and Reset Example

$ron9['A']=1;
$ron9['B']=2;
$ron9['C']=3;

reset ($ron9);
while ( list ($I, $J) = each( $ron9 ))
echo "$I - $J<br />";

Result:

A - 1
B - 2
C - 3

Example 10

$ron10[0][0]='00';
$ron10[1][0]='10';
$ron10[2][0]='20';
$ron10[0][1]='01';
$ron10[1][1]='11';
$ron10[2][1]='21';
$ron10[0][2]='02';
$ron10[1][2]='12';
$ron10[2][2]='22';

echo '$ron10[0][0] = '.$ron10[0][0].'<br />';
echo '$ron10[1][0] = '.$ron10[1][0].'<br />';
echo '$ron10[2][0] = '.$ron10[2][0].'<br />';
echo '$ron10[0][1] = '.$ron10[0][1].'<br />';
echo '$ron10[1][1] = '.$ron10[1][1].'<br />';
echo '$ron10[2][1] = '.$ron10[2][1].'<br />';
echo '$ron10[0][2] = '.$ron10[0][2].'<br />';
echo '$ron10[1][2] = '.$ron10[1][2].'<br />';
echo '$ron10[2][2] = '.$ron10[2][2].'<br />';

 

Result:

$ron10[0][0] = 00
$ron10[1][0] = 10
$ron10[2][0] = 20
$ron10[0][1] = 01
$ron10[1][1] = 11
$ron10[2][1] = 21
$ron10[0][2] = 02
$ron10[1][2] = 12
$ron10[2][2] = 22


 

Valid XHTML 1.0 Transitional        Valid CSS!