PHP & MySQL for Dummies- P5
Số trang: 50
Loại file: pdf
Dung lượng: 0.00 B
Lượt xem: 7
Lượt tải: 0
Xem trước 5 trang đầu tiên của tài liệu này:
Thông tin tài liệu:
Tham khảo tài liệu php & mysql for dummies- p5, công nghệ thông tin, cơ sở dữ liệu phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả
Nội dung trích xuất từ tài liệu:
PHP & MySQL for Dummies- P5 Chapter 7: PHP Building Blocks for Programs 181 ✓ previous($arrayname): Moves the pointer to the value before the current pointer location ✓ end($arrayname): Moves the pointer to the last value in the array ✓ reset($arrayname): Moves the pointer to the first value in the array The following statements manually walk through an array containing state capitals: $value = current ($capitals); echo “$value”; $value = next ($capitals); echo “$value”; $value = next ($capitals); echo “$value”; Unless you moved the pointer previously, the pointer is located at the first element when you start walking through the array. If you think that the array pointer may have been moved earlier in the script or if your output from the array seems to start somewhere in the middle, use the reset statement before you start walking, as follows: reset($capitals); When using this method to walk through an array, you need an assignment statement and an echo statement for every value in the array — for each of the 50 states. The output is a list of all the state capitals. This method gives you flexibility. You can move through the array in any manner — not just one value at a time. You can move backwards, go directly to the end, skip every other value by using two next statements in a row, or whatever method is useful. However, if you want to go through the array from beginning to end, one value at a time, PHP provides foreach, which does exactly what you need much more efficiently. foreach is described in the next section. Using foreach to walk through an array foreach walks through the array one value at a time. The current key and value of the array can be used in the block of statements each time the block executes. The general format is foreach( $arrayname as $keyname => $valuename ) { block of statements; }Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 182 Part III: PHP Fill in the following information: ✓ arrayname: The name of the array that you’re walking through. ✓ keyname: The name of the variable where you want to store the key. keyname is optional. If you leave out $keyname =>, only the value is put into a variable that can be used in the block of statements. ✓ valuename: The name of the variable where you want to store the value. For instance, the following foreach statement walks through the sample array of state capitals and echoes a list: $capitals = array(“CA” => “Sacramento”, “TX” => “Austin”, “OR” => “Salem” ); ksort($capitals); foreach( $capitals as $state => $city ) { echo “$city, $state”; } The preceding statements give the following Web page output: Sacramento, CA Salem, OR Austin, TX You can use the following line in place of the foreach line in the previous statements: foreach( $capitals as $city ) When using this foreach statement, only the city is available for output. You would then use the following echo statement: echo “$city”; The output with these changes is Sacramento Salem Austin When foreach starts walking through an array, it moves the pointer to the beginning of the array. You don’t need to reset an array before walking through it with foreach.Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. ...
Nội dung trích xuất từ tài liệu:
PHP & MySQL for Dummies- P5 Chapter 7: PHP Building Blocks for Programs 181 ✓ previous($arrayname): Moves the pointer to the value before the current pointer location ✓ end($arrayname): Moves the pointer to the last value in the array ✓ reset($arrayname): Moves the pointer to the first value in the array The following statements manually walk through an array containing state capitals: $value = current ($capitals); echo “$value”; $value = next ($capitals); echo “$value”; $value = next ($capitals); echo “$value”; Unless you moved the pointer previously, the pointer is located at the first element when you start walking through the array. If you think that the array pointer may have been moved earlier in the script or if your output from the array seems to start somewhere in the middle, use the reset statement before you start walking, as follows: reset($capitals); When using this method to walk through an array, you need an assignment statement and an echo statement for every value in the array — for each of the 50 states. The output is a list of all the state capitals. This method gives you flexibility. You can move through the array in any manner — not just one value at a time. You can move backwards, go directly to the end, skip every other value by using two next statements in a row, or whatever method is useful. However, if you want to go through the array from beginning to end, one value at a time, PHP provides foreach, which does exactly what you need much more efficiently. foreach is described in the next section. Using foreach to walk through an array foreach walks through the array one value at a time. The current key and value of the array can be used in the block of statements each time the block executes. The general format is foreach( $arrayname as $keyname => $valuename ) { block of statements; }Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 182 Part III: PHP Fill in the following information: ✓ arrayname: The name of the array that you’re walking through. ✓ keyname: The name of the variable where you want to store the key. keyname is optional. If you leave out $keyname =>, only the value is put into a variable that can be used in the block of statements. ✓ valuename: The name of the variable where you want to store the value. For instance, the following foreach statement walks through the sample array of state capitals and echoes a list: $capitals = array(“CA” => “Sacramento”, “TX” => “Austin”, “OR” => “Salem” ); ksort($capitals); foreach( $capitals as $state => $city ) { echo “$city, $state”; } The preceding statements give the following Web page output: Sacramento, CA Salem, OR Austin, TX You can use the following line in place of the foreach line in the previous statements: foreach( $capitals as $city ) When using this foreach statement, only the city is available for output. You would then use the following echo statement: echo “$city”; The output with these changes is Sacramento Salem Austin When foreach starts walking through an array, it moves the pointer to the beginning of the array. You don’t need to reset an array before walking through it with foreach.Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. ...
Tìm kiếm theo từ khóa liên quan:
thủ thuật máy tính công nghệ thông tin tin học quản trị mạng computer networkTài liệu liên quan:
-
52 trang 434 1 0
-
24 trang 359 1 0
-
Top 10 mẹo 'đơn giản nhưng hữu ích' trong nhiếp ảnh
11 trang 320 0 0 -
Làm việc với Read Only Domain Controllers
20 trang 310 0 0 -
74 trang 303 0 0
-
96 trang 299 0 0
-
Báo cáo thực tập thực tế: Nghiên cứu và xây dựng website bằng Wordpress
24 trang 291 0 0 -
Đồ án tốt nghiệp: Xây dựng ứng dụng di động android quản lý khách hàng cắt tóc
81 trang 286 0 0 -
EBay - Internet và câu chuyện thần kỳ: Phần 1
143 trang 277 0 0 -
Tài liệu hướng dẫn sử dụng thư điện tử tài nguyên và môi trường
72 trang 270 0 0