- A
- $arr =array (' a ' = ' = ' abc ', ' B ' =>123, ' C ' =>true);
- B
- $arr =range (' A ', ' d ');
- 1
- for ($i =0; $i<>
- echo $arr [$i]. ', ';
- Echo '
';
- 2
- foreach ($arr as $key)
- echo "$key,";
- Echo '
';
- 3
- foreach ($arr as $key = $val)
- echo "$key-$val,";
- Echo '
';
- 4
- Reset ($arr);
- while ($item =each ($arr)) {
- echo $item [' key ']. ' -'. $item [' value ']. ', ';
- }
- Echo '
';
- 5
- Reset ($arr);
- while (list ($key, $val) =each ($arr)) {
- echo "$key-$val,";
- }
- Echo '
';
- ?>
Copy CodeCode Description: Statement a $arr =array (' a ' = ' = ' abc ', ' B ' =>123, ' C ' =>true); Initialize the $arr to get a numeric index array, output:,,, ABC, 123, 1,A-ABC, b-123, C-1,A-ABC, b-123, C-1,A-ABC, b-123, c-1, using statement b $arr =range (' A ', ' d '); Initialize the $arr to get an associative array, output: 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, D-Z, for loop only limited to numeric index; for and fore The reset () operation of the data does not need to be performed at the end of the ACH traversal for the next traversal, which is required by each method.The above is the full content of today's PHP tutorial, hope to help you master the method of the PHP array traversal, the programmer's home, I wish you all learning progress. |