edainworks.com :: VGR :: testing array traverse (parsing) depending on construction method.

First ed. 2006

1st array construction method :
$thearr=array('key1'=>'val1','key2'=>'val2');
$thearr[0]='val0';
$thearr[5]='val5';
Array
(
    [key1] => val1
    [key2] => val2
    [0] => val0
    [5] => val5
)
4 elements in array

value-pair array
construction method :
$thearr=array('key1'=>'val1','key2'=>'val2');
Array
(
    [key1] => val1
    [key2] => val2
)

array parsing via foreach() on key=>val pairs
code :
foreach ($thearr as $key=>$val) echo "$key ... $val<br>";
key1 ... val1
key2 ... val2

array parsing via for()
code :
$j=count($thearr);
for ($i=0;$i<$j;$i++) echo "{$i} ... {$thearr[$i][1]}<br>";
0 ...
1 ...

with a different array construction method
construction method :
$thearr=array();
$thearr[]=array('key1'=>'val1');
$thearr[]=array('key2'=>'val2');
Array
(
    [0] => Array
        (
            [key1] => val1
        )

    [1] => Array
        (
            [key2] => val2
        )

)

foreach on pairs
0 ... Array
1 ... Array

for on array
code :
$j=count($thearr);
for ($i=0;$i<$j;$i++) echo "{$i} ... {$thearr[$i][0]}<br>";
0 ...
1 ...

with a different array construction method
construction method :
$thearr=array();
$thearr['key1']='val1';
$thearr['key2']='val2';
Array
(
    [key1] => val1
    [key2] => val2
)

foreach on pairs
key1 ... val1
key2 ... val2

for on array
code :
$j=count($thearr);
for ($i=0;$i<$j;$i++) echo "{$i} ... {$thearr[$i][0]}<br>";
0 ...
1 ...

Vincent Graux (VGR) for European Experts Exchange and Edažn Works  back to list of test scripts
Last update 2024-05-13 15:48:06