Trong bài viết này, chúng ta sẽ xem cách truy cập vào phần tử đầu tiên của một mảng trong PHP, đồng thời hiểu cách triển khai chúng thông qua các ví dụ. Có 3 loại mảng chủ yếu trong PHP có thể được sử dụng để tìm nạp các phần tử từ mảng
- Mảng được lập chỉ mục. Nó là một mảng với một phím số. Về cơ bản, nó là một mảng trong đó mỗi khóa được liên kết với giá trị cụ thể của chính nó
- Mảng kết hợp. Nó được sử dụng để lưu trữ các cặp khóa-giá trị
- Mảng đa chiều. Nó là một loại mảng lưu trữ một mảng khác tại mỗi chỉ mục thay vì một phần tử. Nói cách khác, định nghĩa mảng nhiều chiều là mảng của mảng.
Có một số phương pháp để lấy phần tử đầu tiên của một mảng trong PHP. Một số phương thức đang sử dụng vòng lặp foreach, hàm đặt lại, hàm array_slice, , array_reverse, v.v. Chúng ta sẽ thảo luận về các cách khác nhau để truy cập phần tử đầu tiên của một mảng một cách tuần tự
Bằng cách truy cập trực tiếp vào chỉ mục thứ 0
PHP
foreach[ $array as $key => $element] { // PHP Code to be executed }1
foreach[ $array as $key => $element] { // PHP Code to be executed }2
foreach[ $array as $key => $element] { // PHP Code to be executed }3____04
foreach[ $array as $key => $element] { // PHP Code to be executed }3____06
foreach[ $array as $key => $element] { // PHP Code to be executed }3_______08
foreach[ $array as $key => $element] { // PHP Code to be executed }9____90
geeks1____92
geeks3
geeks4
geeks3
geeks6
geeks7
foreach[ $array as $key => $element] { // PHP Code to be executed }3
geeks9
foreach[ $array as $key => $element] { // PHP Code to be executed }8
foreach[ $array as $key => $element] { // PHP Code to be executed }01
foreach[ $array as $key => $element] { // PHP Code to be executed }02
đầu ra
geeks
Sử dụng vòng lặp foreach. Cấu trúc foreach hoạt động trên cả mảng và đối tượng. Vòng lặp foreach lặp qua một mảng các phần tử, việc thực thi được đơn giản hóa và kết thúc vòng lặp trong thời gian tương đối ngắn hơn
cú pháp
foreach[ $array as $key => $element] { // PHP Code to be executed }
Ví dụ. Ví dụ này minh họa vòng lặp foreach trong PHP
PHP
foreach[ $array as $key => $element] { // PHP Code to be executed }03
foreach[ $array as $key => $element] { // PHP Code to be executed }04
foreach[ $array as $key => $element] { // PHP Code to be executed }3____04
foreach[ $array as $key => $element] { // PHP Code to be executed }3____06
foreach[ $array as $key => $element] { // PHP Code to be executed }3
foreach[ $array as $key => $element] { // PHP Code to be executed }8
foreach[ $array as $key => $element] { // PHP Code to be executed }9
geeks0
geeks1
geeks4
geeks5
geeks2
geeks7
geeks4
geeks9
geeks4
geeks7
geeks4____443
geeks6
foreach[ $array as $key => $element] { // PHP Code to be executed }3
geeks7
foreach[ $array as $key => $element] { // PHP Code to be executed }04
foreach[ $array as $key => $element] { // PHP Code to be executed }3______449
geeks1
foreach[ $array as $key => $element] { // PHP Code to be executed }8
geeks2
geeks3
geeks4
geeks4
geeks9
geeks3_______98
foreach[ $array as $key => $element] { // PHP Code to be executed }04
geeks4
foreach[ $array as $key => $element] { // PHP Code to be executed }21
geeks4
foreach[ $array as $key => $element] { // PHP Code to be executed }23
foreach[ $array as $key => $element] { // PHP Code to be executed }24
_______03____626
foreach[ $array as $key => $element] { // PHP Code to be executed }02
đầu ra
geeks
Sử dụng chức năng đặt lại []. Hàm reset[] dùng để di chuyển con trỏ trong của mảng đến phần tử đầu tiên
cú pháp
foreach[ $array as $key => $element] { // PHP Code to be executed }0
Ví dụ. Ví dụ này minh họa việc sử dụng hàm reset[] giúp di chuyển con trỏ bên trong của bất kỳ mảng nào đến phần tử đầu tiên của mảng đó
PHP
foreach[ $array as $key => $element] { // PHP Code to be executed }03
foreach[ $array as $key => $element] { // PHP Code to be executed }3____04
foreach[ $array as $key => $element] { // PHP Code to be executed }3____06
foreach[ $array as $key => $element] { // PHP Code to be executed }3
foreach[ $array as $key => $element] { // PHP Code to be executed }8
foreach[ $array as $key => $element] { // PHP Code to be executed }9
geeks0
geeks1
geeks4
geeks5
geeks2
geeks7
geeks4
geeks9
geeks4
geeks7
geeks4____443
geeks6
foreach[ $array as $key => $element] { // PHP Code to be executed }3
geeks7
foreach[ $array as $key => $element] { // PHP Code to be executed }3
geeks9
foreach[ $array as $key => $element] { // PHP Code to be executed }03
foreach[ $array as $key => $element] { // PHP Code to be executed }8
geeks7
foreach[ $array as $key => $element] { // PHP Code to be executed }02
đầu ra
geeks
Sử dụng hàm array_slice[]. array_slice[] trả về chuỗi các phần tử từ mảng như được chỉ định bởi các tham số offset và độ dài
cú pháp
foreach[ $array as $key => $element] { // PHP Code to be executed }4
Ví dụ. Ví dụ này minh họa Hàm array_slice[] để lấy một phần của mảng bằng cách cắt qua nó, theo lựa chọn của người dùng
PHP
foreach[ $array as $key => $element] { // PHP Code to be executed }1
foreach[ $array as $key => $element] { // PHP Code to be executed }2
foreach[ $array as $key => $element] { // PHP Code to be executed }3____04
foreach[ $array as $key => $element] { // PHP Code to be executed }3____06
foreach[ $array as $key => $element] { // PHP Code to be executed }3
foreach[ $array as $key => $element] { // PHP Code to be executed }8
foreach[ $array as $key => $element] { // PHP Code to be executed }9
geeks0
geeks1
geeks4
geeks5
geeks2
geeks7
geeks4
geeks9
geeks4
geeks7
geeks4____443
geeks6
foreach[ $array as $key => $element] { // PHP Code to be executed }3
geeks7
foreach[ $array as $key => $element] { // PHP Code to be executed }3______99
foreach[ $array as $key => $element] { // PHP Code to be executed }33____91
foreach[ $array as $key => $element] { // PHP Code to be executed }8
foreach[ $array as $key => $element] { // PHP Code to be executed }36
foreach[ $array as $key => $element] { // PHP Code to be executed }02
đầu ra
geeks
sử dụng. Hàm này trả về một mảng các giá trị từ một mảng khác có thể chứa các cặp khóa-giá trị hoặc chỉ các giá trị
cú pháp
foreach[ $array as $key => $element] { // PHP Code to be executed }2
Ví dụ. Ví dụ này mô tả việc sử dụng hàm array_values[]
PHP
foreach[ $array as $key => $element] { // PHP Code to be executed }1
foreach[ $array as $key => $element] { // PHP Code to be executed }2
foreach[ $array as $key => $element] { // PHP Code to be executed }3____04
foreach[ $array as $key => $element] { // PHP Code to be executed }3____06
foreach[ $array as $key => $element] { // PHP Code to be executed }3
foreach[ $array as $key => $element] { // PHP Code to be executed }8
foreach[ $array as $key => $element] { // PHP Code to be executed }9
geeks0
geeks1
geeks4
geeks5
geeks2
geeks7
geeks4
geeks9
geeks4
geeks7
geeks4____443
geeks6
foreach[ $array as $key => $element] { // PHP Code to be executed }3
geeks7
foreach[ $array as $key => $element] { // PHP Code to be executed }3______99
foreach[ $array as $key => $element] { // PHP Code to be executed }64
geeks1
foreach[ $array as $key => $element] { // PHP Code to be executed }8
foreach[ $array as $key => $element] { // PHP Code to be executed }67
foreach[ $array as $key => $element] { // PHP Code to be executed }02
đầu ra
geeks
Sử dụng hàm array_pop[]. Hàm này bật phần tử ra khỏi cuối mảng
cú pháp
foreach[ $array as $key => $element] { // PHP Code to be executed }67
Theo mặc định, hàm array_reverse[] sẽ đặt lại tất cả các khóa mảng số để bắt đầu đếm từ 0 trong khi các khóa ký tự sẽ không thay đổi trừ khi tham số thứ haiserve_keys được chỉ định là TRUE. Phương pháp này không được khuyến nghị vì nó có thể khiến quá trình xử lý lâu hơn không mong muốn trên các mảng lớn hơn để đảo ngược chúng trước khi nhận được giá trị đầu tiên
Ví dụ. Ví dụ này mô tả việc sử dụng hàm array_pop
PHP
foreach[ $array as $key => $element] { // PHP Code to be executed }1
foreach[ $array as $key => $element] { // PHP Code to be executed }70
foreach[ $array as $key => $element] { // PHP Code to be executed }71
foreach[ $array as $key => $element] { // PHP Code to be executed }70
foreach[ $array as $key => $element] { // PHP Code to be executed }6
foreach[ $array as $key => $element] { // PHP Code to be executed }70
foreach[ $array as $key => $element] { // PHP Code to be executed }8
foreach[ $array as $key => $element] { // PHP Code to be executed }9
geeks0
geeks1
foreach[ $array as $key => $element] { // PHP Code to be executed }79
geeks5
geeks2
geeks3
foreach[ $array as $key => $element] { // PHP Code to be executed }79
geeks9
geeks4
geeks3
_______079____443
geeks6
foreach[ $array as $key => $element] { // PHP Code to be executed }70
geeks7
foreach[ $array as $key => $element] { // PHP Code to be executed }70
foreach[ $array as $key => $element] { // PHP Code to be executed }93
foreach[ $array as $key => $element] { // PHP Code to be executed }9
foreach[ $array as $key => $element] { // PHP Code to be executed }95
geeks1
foreach[ $array as $key => $element] { // PHP Code to be executed }8
geeks7
foreach[ $array as $key => $element] { // PHP Code to be executed }70
geeks9
geeks01
geeks1
foreach[ $array as $key => $element] { // PHP Code to be executed }93
geeks7
foreach[ $array as $key => $element] { // PHP Code to be executed }02
đầu ra
geeks
PHP là ngôn ngữ kịch bản phía máy chủ được thiết kế dành riêng cho phát triển web. Bạn có thể học PHP từ đầu bằng cách làm theo Hướng dẫn PHP và Ví dụ về PHP này