Mảng PHP thực sự là bản đồ có thứ tự, nghĩa là tất cả các giá trị của mảng đều có khóa và các mục bên trong mảng giữ nguyên thứ tự. Khi sử dụng mảng dưới dạng danh sách đơn giản như chúng ta đã thấy ở chương trước, bộ đếm dựa trên số 0 được sử dụng để đặt khóa. Mỗi mục được thêm vào mảng sẽ tăng chỉ số tiếp theo lên 1
Một ví dụ tốt cho việc sử dụng mảng với các phím là danh bạ điện thoại. Giả sử chúng ta muốn lưu số điện thoại của những người trong một lớp học
$phone_numbers = [
"Alex" => "415-235-8573",
"Jessica" => "415-492-4856",
];
print_r[$phone_numbers];
echo "Alex's phone number is " . $phone_numbers["Alex"] . "\n";
echo "Jessica's phone number is " . $phone_numbers["Jessica"] . "\n";
Để thêm một mục vào một mảng bằng một khóa, chúng tôi sử dụng toán tử dấu ngoặc, như bạn mong đợi
$phone_numbers = [
"Alex" => "415-235-8573",
"Jessica" => "415-492-4856",
];
$phone_numbers["Michael"] = "415-955-3857";
print_r[$phone_numbers];
Để kiểm tra xem một khóa có tồn tại trong một mảng hay không, chúng ta có thể sử dụng hàm
$phone_numbers = [
"Alex" => "415-235-8573",
"Jessica" => "415-492-4856",
];
print_r[array_keys[$phone_numbers]];
2$phone_numbers = [
"Alex" => "415-235-8573",
"Jessica" => "415-492-4856",
];
if [array_key_exists["Alex", $phone_numbers]] {
echo "Alex's phone number is " . $phone_numbers["Alex"] . "\n";
} else {
echo "Alex's phone number is not in the phone book!";
}
if [array_key_exists["Michael", $phone_numbers]] {
echo "Michael's phone number is " . $phone_numbers["Michael"] . "\n";
} else {
echo "Michael's phone number is not in the phone book!";
}
Nếu chúng ta chỉ muốn trích xuất các khóa của mảng [tên], chúng ta có thể sử dụng hàm
$phone_numbers = [
"Alex" => "415-235-8573",
"Jessica" => "415-492-4856",
];
print_r[array_keys[$phone_numbers]];
3$phone_numbers = [
"Alex" => "415-235-8573",
"Jessica" => "415-492-4856",
];
print_r[array_keys[$phone_numbers]];
Ngoài ra, để chỉ lấy các giá trị của một mảng [các số điện thoại], chúng ta có thể sử dụng hàm
$phone_numbers = [
"Alex" => "415-235-8573",
"Jessica" => "415-492-4856",
];
print_r[array_keys[$phone_numbers]];
4$phone_numbers = [
"Alex" => "415-235-8573",
"Jessica" => "415-492-4856",
];
print_r[array_values[$phone_numbers]];
Tập thể dục
Thêm một số vào danh bạ điện thoại cho Eric, với số 415-874-7659, bằng cách thêm nó vào định nghĩa mảng hoặc dưới dạng một dòng mã riêng
Ghi chú. Nếu giá_trị_khóa_tìm_kiếm tùy chọn được chỉ định, thì chỉ các khóa cho giá trị đó được trả về. Mặt khác, tất cả các khóa từ mảng được trả về
Thông số
NameDescriptionRequired /
OptionalTypeinput_arrayMảng được chỉ định. Bắt buộcArraysearch_key_valueGiá trị cần kiểm tra. Tùy chọnArraystrictKể từ PHP 5, tham số này xác định xem có nên sử dụng phép so sánh nghiêm ngặt [===] trong quá trình tìm kiếm hay không. Boolean tùy chọn
Giá trị trả về
Một mảng gồm tất cả các khóa của input_arrray
Loại giá trị. Mảng
Ví dụ 1
đầu ra
Array [ [0] => Orange [1] => Apple [2] => Banana [3] => Cherry ]
Trình bày bằng hình ảnh
Xem ví dụ trên trình duyệt
Ví dụ - 2
đầu ra
Mảng [ [0] => 1 [1] => 3 ]
Xem ví dụ trên trình duyệt
Thực hành trực tuyến tại đây
Xem thêm
Tham chiếu hàm PHP
Trước. array_key_exists
Tiếp theo. mảng_bản đồ
PHP. Lời khuyên trong ngày
PHP. Tiếng vang và bản in khác nhau như thế nào trong PHP?
1. Tốc độ, vận tốc. Có một sự khác biệt giữa hai loại này, nhưng về tốc độ, nó không liên quan đến việc bạn sử dụng loại nào. echo nhanh hơn một chút vì nó không đặt giá trị trả về nếu bạn thực sự muốn đi sâu vào vấn đề
2. Sự biểu lộ. print[] hoạt động giống như một chức năng mà bạn có thể làm. $ret = in "Xin chào thế giới"; . Điều đó có nghĩa là bản in có thể được sử dụng như một phần của biểu thức phức tạp hơn mà tiếng vang không thể. Một ví dụ từ Cẩm nang PHP
print cũng là một phần của bảng ưu tiên mà nó cần phải có nếu nó được sử dụng trong một biểu thức phức tạp. Mặc dù nó chỉ nằm ở cuối danh sách ưu tiên. Chỉ , AND OR XOR thấp hơn
3. Thông số]. ngữ pháp là. biểu thức tiếng vang [, biểu thức [, biểu thức]. ] Nhưng echo [ biểu thức, biểu thức ] không hợp lệ. Điều này sẽ hợp lệ. echo ["chào bạn"],["đối tác"]; . lặp lại "xin chào","đối tác"; . ]
Array_keys[] là một hàm tích hợp trong PHP và được sử dụng để trả về tất cả các khóa của và mảng hoặc tập hợp con của các khóa
cú pháp
array array_keys[$input_array, $search_value, $strict]
Thông số. Hàm lấy ba tham số trong đó một tham số là bắt buộc và hai tham số còn lại là tùy chọn
- $input_array [bắt buộc]. Đề cập đến mảng mà chúng tôi muốn hoạt động trên
- $search_value [tùy chọn]. Đề cập đến giá trị của mảng mà chúng ta muốn tìm kiếm các phần tử chính trong mảng. Nếu tham số này được truyền vào thì hàm sẽ chỉ trả về các khóa tương ứng với phần tử này, ngược lại nó sẽ trả về tất cả các khóa của mảng
- $strict [tùy chọn]. Xác định xem có nên sử dụng phép so sánh nghiêm ngặt [===] trong quá trình tìm kiếm hay không. sai là giá trị mặc định
Giá trị trả về. Hàm trả về một mảng chứa tất cả các khóa hoặc tập hợp con của các khóa, mảng đầu vào tùy thuộc vào các tham số được truyền
ví dụ
$phone_numbers = [
"Alex" => "415-235-8573",
"Jessica" => "415-492-4856",
];
$phone_numbers["Michael"] = "415-955-3857";
print_r[$phone_numbers];
0Trong chương trình dưới đây, chúng ta đã truyền một mảng kết hợp đơn giản cho hàm array_keys[], để in tất cả các khóa của nó
$phone_numbers = [
"Alex" => "415-235-8573",
"Jessica" => "415-492-4856",
];
print_r[array_keys[$phone_numbers]];
5$phone_numbers = [
"Alex" => "415-235-8573",
"Jessica" => "415-492-4856",
];
print_r[array_keys[$phone_numbers]];
6$phone_numbers = [
"Alex" => "415-235-8573",
"Jessica" => "415-492-4856",
];
print_r[array_keys[$phone_numbers]];
7$phone_numbers = [
"Alex" => "415-235-8573",
"Jessica" => "415-492-4856",
];
print_r[array_keys[$phone_numbers]];
8 $phone_numbers = [
"Alex" => "415-235-8573",
"Jessica" => "415-492-4856",
];
print_r[array_keys[$phone_numbers]];
9____40$phone_numbers = [
"Alex" => "415-235-8573",
"Jessica" => "415-492-4856",
];
$phone_numbers["Michael"] = "415-955-3857";
print_r[$phone_numbers];
00$phone_numbers = [
"Alex" => "415-235-8573",
"Jessica" => "415-492-4856",
];
$phone_numbers["Michael"] = "415-955-3857";
print_r[$phone_numbers];
01$phone_numbers = [
"Alex" => "415-235-8573",
"Jessica" => "415-492-4856",
];
$phone_numbers["Michael"] = "415-955-3857";
print_r[$phone_numbers];
02$phone_numbers = [
"Alex" => "415-235-8573",
"Jessica" => "415-492-4856",
];
$phone_numbers["Michael"] = "415-955-3857";
print_r[$phone_numbers];
03 $phone_numbers = [
"Alex" => "415-235-8573",
"Jessica" => "415-492-4856",
];
$phone_numbers["Michael"] = "415-955-3857";
print_r[$phone_numbers];
04$phone_numbers = [
"Alex" => "415-235-8573",
"Jessica" => "415-492-4856",
];
$phone_numbers["Michael"] = "415-955-3857";
print_r[$phone_numbers];
05$phone_numbers = [
"Alex" => "415-235-8573",
"Jessica" => "415-492-4856",
];
$phone_numbers["Michael"] = "415-955-3857";
print_r[$phone_numbers];
06$phone_numbers = [
"Alex" => "415-235-8573",
"Jessica" => "415-492-4856",
];
print_r[array_values[$phone_numbers]];
0$phone_numbers = [
"Alex" => "415-235-8573",
"Jessica" => "415-492-4856",
];
$phone_numbers["Michael"] = "415-955-3857";
print_r[$phone_numbers];
08$phone_numbers = [
"Alex" => "415-235-8573",
"Jessica" => "415-492-4856",
];
$phone_numbers["Michael"] = "415-955-3857";
print_r[$phone_numbers];
02$phone_numbers = [
"Alex" => "415-235-8573",
"Jessica" => "415-492-4856",
];
if [array_key_exists["Alex", $phone_numbers]] {
echo "Alex's phone number is " . $phone_numbers["Alex"] . "\n";
} else {
echo "Alex's phone number is not in the phone book!";
}
if [array_key_exists["Michael", $phone_numbers]] {
echo "Michael's phone number is " . $phone_numbers["Michael"] . "\n";
} else {
echo "Michael's phone number is not in the phone book!";
}
00____106$phone_numbers = [
"Alex" => "415-235-8573",
"Jessica" => "415-492-4856",
];
$phone_numbers["Michael"] = "415-955-3857";
print_r[$phone_numbers];
03$phone_numbers = [
"Alex" => "415-235-8573",
"Jessica" => "415-492-4856",
];
$phone_numbers["Michael"] = "415-955-3857";
print_r[$phone_numbers];
08$phone_numbers = [
"Alex" => "415-235-8573",
"Jessica" => "415-492-4856",
];
if [array_key_exists["Alex", $phone_numbers]] {
echo "Alex's phone number is " . $phone_numbers["Alex"] . "\n";
} else {
echo "Alex's phone number is not in the phone book!";
}
if [array_key_exists["Michael", $phone_numbers]] {
echo "Michael's phone number is " . $phone_numbers["Michael"] . "\n";
} else {
echo "Michael's phone number is not in the phone book!";
}
04$phone_numbers = [
"Alex" => "415-235-8573",
"Jessica" => "415-492-4856",
];
print_r[array_keys[$phone_numbers]];
6$phone_numbers = [
"Alex" => "415-235-8573",
"Jessica" => "415-492-4856",
];
print_r[array_values[$phone_numbers]];
0 $phone_numbers = [
"Alex" => "415-235-8573",
"Jessica" => "415-492-4856",
];
$phone_numbers["Michael"] = "415-955-3857";
print_r[$phone_numbers];
04$phone_numbers = [
"Alex" => "415-235-8573",
"Jessica" => "415-492-4856",
];
if [array_key_exists["Alex", $phone_numbers]] {
echo "Alex's phone number is " . $phone_numbers["Alex"] . "\n";
} else {
echo "Alex's phone number is not in the phone book!";
}
if [array_key_exists["Michael", $phone_numbers]] {
echo "Michael's phone number is " . $phone_numbers["Michael"] . "\n";
} else {
echo "Michael's phone number is not in the phone book!";
}
08$phone_numbers = [
"Alex" => "415-235-8573",
"Jessica" => "415-492-4856",
];
$phone_numbers["Michael"] = "415-955-3857";
print_r[$phone_numbers];
06$phone_numbers = [
"Alex" => "415-235-8573",
"Jessica" => "415-492-4856",
];
if [array_key_exists["Alex", $phone_numbers]] {
echo "Alex's phone number is " . $phone_numbers["Alex"] . "\n";
} else {
echo "Alex's phone number is not in the phone book!";
}
if [array_key_exists["Michael", $phone_numbers]] {
echo "Michael's phone number is " . $phone_numbers["Michael"] . "\n";
} else {
echo "Michael's phone number is not in the phone book!";
}
80$phone_numbers = [
"Alex" => "415-235-8573",
"Jessica" => "415-492-4856",
];
print_r[array_keys[$phone_numbers]];
62$phone_numbers = [
"Alex" => "415-235-8573",
"Jessica" => "415-492-4856",
];
print_r[array_keys[$phone_numbers]];
88$phone_numbers = [
"Alex" => "415-235-8573",
"Jessica" => "415-492-4856",
];
print_r[array_keys[$phone_numbers]];
62$phone_numbers = [
"Alex" => "415-235-8573",
"Jessica" => "415-492-4856",
];
if [array_key_exists["Alex", $phone_numbers]] {
echo "Alex's phone number is " . $phone_numbers["Alex"] . "\n";
} else {
echo "Alex's phone number is not in the phone book!";
}
if [array_key_exists["Michael", $phone_numbers]] {
echo "Michael's phone number is " . $phone_numbers["Michael"] . "\n";
} else {
echo "Michael's phone number is not in the phone book!";
}
87$phone_numbers = [
"Alex" => "415-235-8573",
"Jessica" => "415-492-4856",
];
print_r[array_keys[$phone_numbers]];
62$phone_numbers = [
"Alex" => "415-235-8573",
"Jessica" => "415-492-4856",
];
if [array_key_exists["Alex", $phone_numbers]] {
echo "Alex's phone number is " . $phone_numbers["Alex"] . "\n";
} else {
echo "Alex's phone number is not in the phone book!";
}
if [array_key_exists["Michael", $phone_numbers]] {
echo "Michael's phone number is " . $phone_numbers["Michael"] . "\n";
} else {
echo "Michael's phone number is not in the phone book!";
}
80$phone_numbers = [
"Alex" => "415-235-8573",
"Jessica" => "415-492-4856",
];
print_r[array_keys[$phone_numbers]];
62$phone_numbers = [
"Alex" => "415-235-8573",
"Jessica" => "415-492-4856",
];
print_r[array_keys[$phone_numbers]];
94$phone_numbers = [
"Alex" => "415-235-8573",
"Jessica" => "415-492-4856",
];
if [array_key_exists["Alex", $phone_numbers]] {
echo "Alex's phone number is " . $phone_numbers["Alex"] . "\n";
} else {
echo "Alex's phone number is not in the phone book!";
}
if [array_key_exists["Michael", $phone_numbers]] {
echo "Michael's phone number is " . $phone_numbers["Michael"] . "\n";
} else {
echo "Michael's phone number is not in the phone book!";
}
85