Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; WP_MatchesMapRegex has a deprecated constructor in /var/www/phpcoban.com/public_html/wp-includes/class-wp.php on line 632

Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; Translation_Entry has a deprecated constructor in /var/www/phpcoban.com/public_html/wp-includes/pomo/entry.php on line 14

Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; POMO_Reader has a deprecated constructor in /var/www/phpcoban.com/public_html/wp-includes/pomo/streams.php on line 12

Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; POMO_FileReader has a deprecated constructor in /var/www/phpcoban.com/public_html/wp-includes/pomo/streams.php on line 120

Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; POMO_StringReader has a deprecated constructor in /var/www/phpcoban.com/public_html/wp-includes/pomo/streams.php on line 175

Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; POMO_CachedFileReader has a deprecated constructor in /var/www/phpcoban.com/public_html/wp-includes/pomo/streams.php on line 221

Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; POMO_CachedIntFileReader has a deprecated constructor in /var/www/phpcoban.com/public_html/wp-includes/pomo/streams.php on line 236

Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; WP_Widget_Factory has a deprecated constructor in /var/www/phpcoban.com/public_html/wp-includes/widgets.php on line 544

Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; EzAdSense has a deprecated constructor in /var/www/phpcoban.com/public_html/wp-content/plugins/easy-adsense-lite/easy-adsense-lite.php on line 38

Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; EzAdsWidget has a deprecated constructor in /var/www/phpcoban.com/public_html/wp-content/plugins/easy-adsense-lite/easy-adsense-lite.php on line 941

Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; EzAdsSearch has a deprecated constructor in /var/www/phpcoban.com/public_html/wp-content/plugins/easy-adsense-lite/easy-adsense-lite.php on line 1003

Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; EzAdsLU has a deprecated constructor in /var/www/phpcoban.com/public_html/wp-content/plugins/easy-adsense-lite/easy-adsense-lite.php on line 1078

Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; EzBaseOption has a deprecated constructor in /var/www/phpcoban.com/public_html/wp-content/plugins/easy-adsense-lite/EzOptions.php on line 22

Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; EzCheckBox has a deprecated constructor in /var/www/phpcoban.com/public_html/wp-content/plugins/easy-adsense-lite/EzOptions.php on line 174

Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; EzRadioBox has a deprecated constructor in /var/www/phpcoban.com/public_html/wp-content/plugins/easy-adsense-lite/EzOptions.php on line 199

Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; EzSelect has a deprecated constructor in /var/www/phpcoban.com/public_html/wp-content/plugins/easy-adsense-lite/EzOptions.php on line 250

Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; EzMessage has a deprecated constructor in /var/www/phpcoban.com/public_html/wp-content/plugins/easy-adsense-lite/EzOptions.php on line 294

Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; EzHelpTag has a deprecated constructor in /var/www/phpcoban.com/public_html/wp-content/plugins/easy-adsense-lite/EzOptions.php on line 313

Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; EzHelpPopUp has a deprecated constructor in /var/www/phpcoban.com/public_html/wp-content/plugins/easy-adsense-lite/EzOptions.php on line 328

Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; EzTextArea has a deprecated constructor in /var/www/phpcoban.com/public_html/wp-content/plugins/easy-adsense-lite/EzOptions.php on line 346

Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; EzText has a deprecated constructor in /var/www/phpcoban.com/public_html/wp-content/plugins/easy-adsense-lite/EzOptions.php on line 366

Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; EzSubmit has a deprecated constructor in /var/www/phpcoban.com/public_html/wp-content/plugins/easy-adsense-lite/EzOptions.php on line 385

Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; EzColorPicker has a deprecated constructor in /var/www/phpcoban.com/public_html/wp-content/plugins/easy-adsense-lite/EzOptions.php on line 409

Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; EzOneTab has a deprecated constructor in /var/www/phpcoban.com/public_html/wp-content/plugins/easy-adsense-lite/EzOptions.php on line 432

Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; EzMiniTab has a deprecated constructor in /var/www/phpcoban.com/public_html/wp-content/plugins/easy-adsense-lite/EzOptions.php on line 476
Bài 15. Control trong array và foreach - PHP Cơ Bản

Bài 15. Control trong array và foreach

1- Định nghĩa mảng trong PHP:

Với cú pháp giống với java ta có cách gọi 1 mảng như sau:
$tên_biến= array(“Kenny”,”Maria”,”Julia”,”Kenvin”)
Như vậy khi gọi giá trị trong mảng ta có:
Echo $tên_biến[2]; // Giá trị sẽ cho là Julia.
Ngoài cách khai báo ở trên chúng ta cũng có thể khai báo bằng cách khác như sau:

Chúng ta không cần thêm ký tự vào. Vì PHP đã tự xử lý cho chúng ta phần đó.
Để thêm 1 phần tử vào mảng chúng ta cũng có thể sử dụng như sau:
$tên_biến[] = “Jiro”;
Như vậy nếu chúng ta gọi giá trị: echo $tên_biến[4] thì giá trị sẽ là jiro. Vì nó được PHP thêm vào sau cùng và ngầm hiểu là giá trị tiếp theo.

2- Khái niệm mảng kết hợp trong PHP

Là các mảng được tạo index bằng các chuỗi , chúng được gợi là các mảng kết hợp.
Ví dụ:

3- Phép lặp trong mảng:

Cú pháp:

ế tiếp, bạn sẽ xem xét các yếu tố tiếp theo).
Trong đó $array là mảng mà ta muốn thực thi việc lặp dữ liệu, và $temp là một biến trong đó ta sẽ tạm thời lưu trữ mỗi phần tử. Trên mỗi lần lặp, giá trị của phần tử hiện được gán cho $ temp và con trỏ mảng nội bộ được đưa lên bằng một (như vậy lặp k
Ví dụ:

b) Lặp lại qua một mảng kết hợp:

Cú pháp:
Foreach($array as $key=>$value)
{ Hành Động }
Trong đó $array là mảng mà ta muốn thực thi việc lặp dữ liệu, $key là một biến vốn tạm thời chứa mỗi khóa, và $value là một biến vốn tạm thời chữa mỗi giá trị.

Hình thức này bổ sung sẽ chỉ định trong các yếu tố hiện tại của các biến $ key trên mỗi lần lặp.

Ví dụ:

Các ví dụ khác:

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">