The code is as follows:
<?php //a $arr=array('a'=>'abc','b'=>123,'c'=>true); //b //$arr=range('a','d'); //1 for($i=0;$i<sizeof($arr);$i++) echo $arr[$i].', '; echo '<br />'; //2 foreach($arr as $key) echo "$key, "; echo '<br />'; //3 foreach($arr as $key=>$val) echo "$key-$val, "; echo '<br />'; //4 reset($arr); while($item=each($arr)){ echo $item['key'].'-'.$item['value'].', '; } echo '<br />'; //5 reset($arr); while(list($key,$val)=each($arr)){ echo "$key-$val, "; } echo '<br />'; ?>
Use the statement a $arr=array('a'=>'abc','b'=>123,'c'=>true); to initialize $arr to get the number Index array, the output is as follows:
, , ,
abc, 123, 1,
a-abc, b-123, c-1,
a-abc, b-123, c-1,
a-abc, b- 123, c-1, use statement b $arr=range('a','d'); to initialize $arr to get the associative array, the output is as follows:
a, b, c, d,
a, b, c , d,
0-a, 1-b, 2-c, 3-d,
0-a, 1-b, 2-c, 3-d,
0-a, 1-b, 2-c, 3-d, the for loop only has limited numeric indexes; after the for and foreach traversal, the data does not need to be reset() to be available for the next traversal, while the each method does.