- //
- $ 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'
';
- ?>
Code description: Statement a $ arr = array ('a' => 'ABC', 'B' => 123, 'C' => true ); initialize $ arr to get the 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, use Statement B $ arr = range ('A', 'D'); initialize $ arr to get the associated 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, 3-d, the for loop only has limited number indexes. after the for and foreach traversal ends, you do not need to perform the reset () operation on the data for the next traversal, and the each method is required.The above is all the content of today's php Tutorial. I hope to help you master the php array traversal method, programmer's house, and wish you a better learning experience. |