Mảng lấy 10 phần tử đầu php

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
geeks
1____92
geeks
3
geeks
4
geeks
3
geeks
6
geeks
7

foreach[ $array as $key => $element] {
    // PHP Code to be executed
}
3
geeks
9
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
geeks
0
geeks
1

geeks
4
geeks
5
geeks
2
geeks
7

geeks
4
geeks
9
geeks
4
geeks
7

geeks
4____443
geeks
6

foreach[ $array as $key => $element] {
    // PHP Code to be executed
}
3
geeks
7

foreach[ $array as $key => $element] {
    // PHP Code to be executed
}
04

foreach[ $array as $key => $element] {
    // PHP Code to be executed
}
3______449
geeks
1
foreach[ $array as $key => $element] {
    // PHP Code to be executed
}
8
geeks
2
geeks
3
geeks
4

geeks
4
geeks
9
geeks
3_______98

foreach[ $array as $key => $element] {
    // PHP Code to be executed
}
04

geeks
4
foreach[ $array as $key => $element] {
    // PHP Code to be executed
}
21

geeks
4
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
geeks
0
geeks
1

geeks
4
geeks
5
geeks
2
geeks
7

geeks
4
geeks
9
geeks
4
geeks
7

geeks
4____443
geeks
6

foreach[ $array as $key => $element] {
    // PHP Code to be executed
}
3
geeks
7

foreach[ $array as $key => $element] {
    // PHP Code to be executed
}
3
geeks
9
foreach[ $array as $key => $element] {
    // PHP Code to be executed
}
03
foreach[ $array as $key => $element] {
    // PHP Code to be executed
}
8
geeks
7

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
geeks
0
geeks
1

geeks
4
geeks
5
geeks
2
geeks
7

geeks
4
geeks
9
geeks
4
geeks
7

geeks
4____443
geeks
6

foreach[ $array as $key => $element] {
    // PHP Code to be executed
}
3
geeks
7

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
geeks
0
geeks
1

geeks
4
geeks
5
geeks
2
geeks
7

geeks
4
geeks
9
geeks
4
geeks
7

geeks
4____443
geeks
6

foreach[ $array as $key => $element] {
    // PHP Code to be executed
}
3
geeks
7

foreach[ $array as $key => $element] {
    // PHP Code to be executed
}
3______99
foreach[ $array as $key => $element] {
    // PHP Code to be executed
}
64
geeks
1
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
geeks
0
geeks
1

foreach[ $array as $key => $element] {
    // PHP Code to be executed
}
79
geeks
5
geeks
2
geeks
3

foreach[ $array as $key => $element] {
    // PHP Code to be executed
}
79
geeks
9
geeks
4
geeks
3

_______079____443

geeks
6

foreach[ $array as $key => $element] {
    // PHP Code to be executed
}
70
geeks
7

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
geeks
1
foreach[ $array as $key => $element] {
    // PHP Code to be executed
}
8
geeks
7

foreach[ $array as $key => $element] {
    // PHP Code to be executed
}
70
geeks
9
geeks
01
geeks
1
foreach[ $array as $key => $element] {
    // PHP Code to be executed
}
93
geeks
7

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

Làm cách nào để lấy 10 phần tử đầu tiên của mảng trong PHP?

php $input = mảng["a", "b", "c", "d", "e"];

Làm cách nào để lấy 10 phần tử đầu tiên của một mảng?

slice[] method để lấy N phần tử đầu tiên của một mảng, e. g. const first3 = mảng. lát [0, 3]. Mảng. slice[] sẽ trả về một mảng mới chứa N phần tử đầu tiên của mảng ban đầu.

Làm cách nào để lấy 5 phần tử đầu tiên của mảng trong PHP?

Sử dụng hàm array_splice[] .

Làm cách nào để xóa 5 phần tử đầu tiên khỏi mảng trong PHP?

Bạn có thể sử dụng hàm array_shift[] của PHP để xóa phần tử hoặc giá trị đầu tiên khỏi một mảng. Hàm array_shift[] cũng trả về giá trị đã loại bỏ của mảng.

Chủ Đề