Mảng php cùng khóa nhiều giá trị

Trong hướng dẫn này, tôi sẽ tạo một danh sách các hàm mảng PHP phổ biến, với các ví dụ về cách sử dụng và các phương pháp hay nhất. Mọi nhà phát triển PHP phải biết cách sử dụng chúng và cách kết hợp các hàm mảng để làm cho mã dễ đọc và ngắn gọn.

Ngoài ra, còn có một bản trình bày với các ví dụ mã nhất định, vì vậy bạn có thể tải xuống từ các liên kết có liên quan và hiển thị cho đồng nghiệp của mình để xây dựng một nhóm mạnh mẽ hơn

Những thứ cơ bản

Có hai cách khác nhau để tạo mảng. Một là sử dụng 

print_r[array_keys[$array]]; // ['sky', 'grass', 'orange']
print_r[array_values[$array]]; // ['blue', 'green', 'orange']
print_r[array_flip[$array]];

// Array
// [
//     [blue] => sky
//     [green] => grass
//     [orange] => orange
// ]
9 để chỉ định các phần tử là cặp khóa-giá trị. Phương pháp khác là đặt tất cả các phần tử bên trong
$values = ["Apples", "Bananas", "Mangoes", "100", "200"];

if[in_array[100, $values]] {
    echo '100 is one of the values';
}
// 100 is one of the values

if[in_array[200, $values] !== false] {
    echo '200 is not one of the values';
}
// 200 is not one of the values

$values = ["Apples", "Bananas", "Mangoes", "100", "200", 100];

echo array_search[100, $values];
// 3

echo array_search[100, $values, true];
// 5

$values = ["Apples" => 100, "Bananas" => 10, "Mangoes" => 45];

if[array_key_exists["Apples", $values]] {
    echo 'We have apples.';
}
// We have apples.
0. Có hai điểm quan trọng mà bạn nên nhớ khi tạo mảng liên kết với các cặp khóa

Đầu tiên, khóa luôn phải là duy nhất. Nếu bạn cố gắng sử dụng cùng một khóa nhiều lần trong một mảng, PHP sẽ bỏ qua tất cả các cặp khóa-giá trị khác ngoại trừ cặp khóa cuối cùng. Thứ hai, nếu một khóa được tạo dưới dạng float, bool và các biểu diễn chuỗi hợp lệ của số nguyên, thì nó sẽ được chuyển thành số nguyên

Dưới đây là một vài ví dụ về tạo mảng trong PHP

$first = array[10, "Apple", 20, -18, "Monkey"];
print_r[$first];
/* Array
[
    [0] => 10
    [1] => Apple
    [2] => 20
    [3] => -18
    [4] => Monkey
] */

$second = [10, "Apple", 20, -18, "Monkey"];
print_r[$second];
/* Array
[
    [0] => 10
    [1] => Apple
    [2] => 20
    [3] => -18
    [4] => Monkey
] */

$third = [10, 5 => "Apple", 2 => 20, -18, "Monkey"];
print_r[$third];
/* Array
[
    [0] => 10
    [5] => Apple
    [2] => 20
    [6] => -18
    [7] => Monkey
] */

Như bạn có thể thấy, sử dụng

print_r[array_keys[$array]]; // ['sky', 'grass', 'orange']
print_r[array_values[$array]]; // ['blue', 'green', 'orange']
print_r[array_flip[$array]];

// Array
// [
//     [blue] => sky
//     [green] => grass
//     [orange] => orange
// ]
9 hoặc 
$values = ["Apples", "Bananas", "Mangoes", "100", "200"];

if[in_array[100, $values]] {
    echo '100 is one of the values';
}
// 100 is one of the values

if[in_array[200, $values] !== false] {
    echo '200 is not one of the values';
}
// 200 is not one of the values

$values = ["Apples", "Bananas", "Mangoes", "100", "200", 100];

echo array_search[100, $values];
// 3

echo array_search[100, $values, true];
// 5

$values = ["Apples" => 100, "Bananas" => 10, "Mangoes" => 45];

if[array_key_exists["Apples", $values]] {
    echo 'We have apples.';
}
// We have apples.
0 là tương đương khi tạo mảng. Ký hiệu tốc ký đã có sẵn bắt đầu từ PHP 5. 4

Bạn cũng không cần chỉ định khóa cho mọi giá trị mảng. Khi bị bỏ qua, PHP đặt khóa thành nhiều hơn một khóa số nguyên được chỉ định lớn nhất. Tất cả các khóa được gán tự động sẽ lớn hơn hoặc bằng 0

Làm việc với khóa và giá trị

Hãy bắt đầu với các hàm cơ bản hoạt động với các khóa và giá trị của mảng. Một trong số đó là 

$values = ["Apples", "Bananas", "Mangoes", "100", "200"];

if[in_array[100, $values]] {
    echo '100 is one of the values';
}
// 100 is one of the values

if[in_array[200, $values] !== false] {
    echo '200 is not one of the values';
}
// 200 is not one of the values

$values = ["Apples", "Bananas", "Mangoes", "100", "200", 100];

echo array_search[100, $values];
// 3

echo array_search[100, $values, true];
// 5

$values = ["Apples" => 100, "Bananas" => 10, "Mangoes" => 45];

if[array_key_exists["Apples", $values]] {
    echo 'We have apples.';
}
// We have apples.
3, tạo một mảng bằng cách sử dụng một mảng cho các khóa và một mảng khác cho các giá trị của nó

$keys = ['sky', 'grass', 'orange'];
$values = ['blue', 'green', 'orange'];

$array = array_combine[$keys, $values];
print_r[$array];

// Array
// [
//     [sky] => blue
//     [grass] => green
//     [orange] => orange
// ]

Bạn nên biết rằng hàm

$values = ["Apples", "Bananas", "Mangoes", "100", "200"];

if[in_array[100, $values]] {
    echo '100 is one of the values';
}
// 100 is one of the values

if[in_array[200, $values] !== false] {
    echo '200 is not one of the values';
}
// 200 is not one of the values

$values = ["Apples", "Bananas", "Mangoes", "100", "200", 100];

echo array_search[100, $values];
// 3

echo array_search[100, $values, true];
// 5

$values = ["Apples" => 100, "Bananas" => 10, "Mangoes" => 45];

if[array_key_exists["Apples", $values]] {
    echo 'We have apples.';
}
// We have apples.
4 trả về một mảng giá trị được lập chỉ mục, 
$values = ["Apples", "Bananas", "Mangoes", "100", "200"];

if[in_array[100, $values]] {
    echo '100 is one of the values';
}
// 100 is one of the values

if[in_array[200, $values] !== false] {
    echo '200 is not one of the values';
}
// 200 is not one of the values

$values = ["Apples", "Bananas", "Mangoes", "100", "200", 100];

echo array_search[100, $values];
// 3

echo array_search[100, $values, true];
// 5

$values = ["Apples" => 100, "Bananas" => 10, "Mangoes" => 45];

if[array_key_exists["Apples", $values]] {
    echo 'We have apples.';
}
// We have apples.
5 trả về một mảng khóa của một mảng đã cho và 
$values = ["Apples", "Bananas", "Mangoes", "100", "200"];

if[in_array[100, $values]] {
    echo '100 is one of the values';
}
// 100 is one of the values

if[in_array[200, $values] !== false] {
    echo '200 is not one of the values';
}
// 200 is not one of the values

$values = ["Apples", "Bananas", "Mangoes", "100", "200", 100];

echo array_search[100, $values];
// 3

echo array_search[100, $values, true];
// 5

$values = ["Apples" => 100, "Bananas" => 10, "Mangoes" => 45];

if[array_key_exists["Apples", $values]] {
    echo 'We have apples.';
}
// We have apples.
6 trao đổi khóa với giá trị

print_r[array_keys[$array]]; // ['sky', 'grass', 'orange']
print_r[array_values[$array]]; // ['blue', 'green', 'orange']
print_r[array_flip[$array]];

// Array
// [
//     [blue] => sky
//     [green] => grass
//     [orange] => orange
// ]

Bạn có thể kiểm tra xem một mảng có chứa một giá trị cụ thể hay không và lấy khóa tương ứng đầu tiên của nó bằng cách sử dụng hàm 

$values = ["Apples", "Bananas", "Mangoes", "100", "200"];

if[in_array[100, $values]] {
    echo '100 is one of the values';
}
// 100 is one of the values

if[in_array[200, $values] !== false] {
    echo '200 is not one of the values';
}
// 200 is not one of the values

$values = ["Apples", "Bananas", "Mangoes", "100", "200", 100];

echo array_search[100, $values];
// 3

echo array_search[100, $values, true];
// 5

$values = ["Apples" => 100, "Bananas" => 10, "Mangoes" => 45];

if[array_key_exists["Apples", $values]] {
    echo 'We have apples.';
}
// We have apples.
7. Bạn cũng có thể sử dụng 
$values = ["Apples", "Bananas", "Mangoes", "100", "200"];

if[in_array[100, $values]] {
    echo '100 is one of the values';
}
// 100 is one of the values

if[in_array[200, $values] !== false] {
    echo '200 is not one of the values';
}
// 200 is not one of the values

$values = ["Apples", "Bananas", "Mangoes", "100", "200", 100];

echo array_search[100, $values];
// 3

echo array_search[100, $values, true];
// 5

$values = ["Apples" => 100, "Bananas" => 10, "Mangoes" => 45];

if[array_key_exists["Apples", $values]] {
    echo 'We have apples.';
}
// We have apples.
8 nếu bạn chỉ muốn biết liệu một mảng có chứa một phần tử cụ thể hay không và không quan tâm đến vị trí của nó. Cân nhắc sử dụng hàm 
$values = ["Apples", "Bananas", "Mangoes", "100", "200"];

if[in_array[100, $values]] {
    echo '100 is one of the values';
}
// 100 is one of the values

if[in_array[200, $values] !== false] {
    echo '200 is not one of the values';
}
// 200 is not one of the values

$values = ["Apples", "Bananas", "Mangoes", "100", "200", 100];

echo array_search[100, $values];
// 3

echo array_search[100, $values, true];
// 5

$values = ["Apples" => 100, "Bananas" => 10, "Mangoes" => 45];

if[array_key_exists["Apples", $values]] {
    echo 'We have apples.';
}
// We have apples.
9 khi bạn muốn kiểm tra xem mảng có sử dụng một khóa đã cho hay không

$values = ["Apples", "Bananas", "Mangoes", "100", "200"];

if[in_array[100, $values]] {
    echo '100 is one of the values';
}
// 100 is one of the values

if[in_array[200, $values] !== false] {
    echo '200 is not one of the values';
}
// 200 is not one of the values

$values = ["Apples", "Bananas", "Mangoes", "100", "200", 100];

echo array_search[100, $values];
// 3

echo array_search[100, $values, true];
// 5

$values = ["Apples" => 100, "Bananas" => 10, "Mangoes" => 45];

if[array_key_exists["Apples", $values]] {
    echo 'We have apples.';
}
// We have apples.

Như ví dụ trên cho thấy, hãy đảm bảo bạn sử dụng kiểm tra loại nghiêm ngặt nếu bạn không muốn có bất kỳ kết quả không mong muốn nào

Nếu bạn muốn tra cứu nhiều phần tử trong một mảng, thì việc kiểm tra xem mảng đó có chứa một giá trị cụ thể hay không sẽ nhanh hơn bằng cách lật mảng bằng 

$values = ["Apples", "Bananas", "Mangoes", "100", "200"];

if[in_array[100, $values]] {
    echo '100 is one of the values';
}
// 100 is one of the values

if[in_array[200, $values] !== false] {
    echo '200 is not one of the values';
}
// 200 is not one of the values

$values = ["Apples", "Bananas", "Mangoes", "100", "200", 100];

echo array_search[100, $values];
// 3

echo array_search[100, $values, true];
// 5

$values = ["Apples" => 100, "Bananas" => 10, "Mangoes" => 45];

if[array_key_exists["Apples", $values]] {
    echo 'We have apples.';
}
// We have apples.
6 và sau đó sử dụng 
$values = ["Apples", "Bananas", "Mangoes", "100", "200"];

if[in_array[100, $values]] {
    echo '100 is one of the values';
}
// 100 is one of the values

if[in_array[200, $values] !== false] {
    echo '200 is not one of the values';
}
// 200 is not one of the values

$values = ["Apples", "Bananas", "Mangoes", "100", "200", 100];

echo array_search[100, $values];
// 3

echo array_search[100, $values, true];
// 5

$values = ["Apples" => 100, "Bananas" => 10, "Mangoes" => 45];

if[array_key_exists["Apples", $values]] {
    echo 'We have apples.';
}
// We have apples.
9

Làm cho mã của bạn ngắn hơn

Hàm

$values = ["Apples", "Bananas", "Mangoes", "100", "200"];

if[in_array[100, $values]] {
    echo '100 is one of the values';
}
// 100 is one of the values

if[in_array[200, $values] !== false] {
    echo '200 is not one of the values';
}
// 200 is not one of the values

$values = ["Apples", "Bananas", "Mangoes", "100", "200", 100];

echo array_search[100, $values];
// 3

echo array_search[100, $values, true];
// 5

$values = ["Apples" => 100, "Bananas" => 10, "Mangoes" => 45];

if[array_key_exists["Apples", $values]] {
    echo 'We have apples.';
}
// We have apples.
72, không hẳn là một hàm mà là một cấu trúc ngôn ngữ, được thiết kế để gán biến một cách ngắn gọn. Ví dụ: đây là ví dụ cơ bản về cách sử dụng hàm 
$values = ["Apples", "Bananas", "Mangoes", "100", "200"];

if[in_array[100, $values]] {
    echo '100 is one of the values';
}
// 100 is one of the values

if[in_array[200, $values] !== false] {
    echo '200 is not one of the values';
}
// 200 is not one of the values

$values = ["Apples", "Bananas", "Mangoes", "100", "200", 100];

echo array_search[100, $values];
// 3

echo array_search[100, $values, true];
// 5

$values = ["Apples" => 100, "Bananas" => 10, "Mangoes" => 45];

if[array_key_exists["Apples", $values]] {
    echo 'We have apples.';
}
// We have apples.
72 

$values = ["Apples", "Bananas", "Mangoes", "100", "200"];

if[in_array[100, $values]] {
    echo '100 is one of the values';
}
// 100 is one of the values

if[in_array[200, $values] !== false] {
    echo '200 is not one of the values';
}
// 200 is not one of the values

$values = ["Apples", "Bananas", "Mangoes", "100", "200", 100];

echo array_search[100, $values];
// 3

echo array_search[100, $values, true];
// 5

$values = ["Apples" => 100, "Bananas" => 10, "Mangoes" => 45];

if[array_key_exists["Apples", $values]] {
    echo 'We have apples.';
}
// We have apples.
7

Cấu trúc này hoạt động hoàn hảo với các chức năng như 

$values = ["Apples", "Bananas", "Mangoes", "100", "200"];

if[in_array[100, $values]] {
    echo '100 is one of the values';
}
// 100 is one of the values

if[in_array[200, $values] !== false] {
    echo '200 is not one of the values';
}
// 200 is not one of the values

$values = ["Apples", "Bananas", "Mangoes", "100", "200", 100];

echo array_search[100, $values];
// 3

echo array_search[100, $values, true];
// 5

$values = ["Apples" => 100, "Bananas" => 10, "Mangoes" => 45];

if[array_key_exists["Apples", $values]] {
    echo 'We have apples.';
}
// We have apples.
74 hoặc 
$values = ["Apples", "Bananas", "Mangoes", "100", "200"];

if[in_array[100, $values]] {
    echo '100 is one of the values';
}
// 100 is one of the values

if[in_array[200, $values] !== false] {
    echo '200 is not one of the values';
}
// 200 is not one of the values

$values = ["Apples", "Bananas", "Mangoes", "100", "200", 100];

echo array_search[100, $values];
// 3

echo array_search[100, $values, true];
// 5

$values = ["Apples" => 100, "Bananas" => 10, "Mangoes" => 45];

if[array_key_exists["Apples", $values]] {
    echo 'We have apples.';
}
// We have apples.
75. Ngoài ra, bạn có thể bỏ qua một số tham số nếu không cần xác định

$first = array[10, "Apple", 20, -18, "Monkey"];
print_r[$first];
/* Array
[
    [0] => 10
    [1] => Apple
    [2] => 20
    [3] => -18
    [4] => Monkey
] */

$second = [10, "Apple", 20, -18, "Monkey"];
print_r[$second];
/* Array
[
    [0] => 10
    [1] => Apple
    [2] => 20
    [3] => -18
    [4] => Monkey
] */

$third = [10, 5 => "Apple", 2 => 20, -18, "Monkey"];
print_r[$third];
/* Array
[
    [0] => 10
    [5] => Apple
    [2] => 20
    [6] => -18
    [7] => Monkey
] */
2

Ngoài ra, 

$values = ["Apples", "Bananas", "Mangoes", "100", "200"];

if[in_array[100, $values]] {
    echo '100 is one of the values';
}
// 100 is one of the values

if[in_array[200, $values] !== false] {
    echo '200 is not one of the values';
}
// 200 is not one of the values

$values = ["Apples", "Bananas", "Mangoes", "100", "200", 100];

echo array_search[100, $values];
// 3

echo array_search[100, $values, true];
// 5

$values = ["Apples" => 100, "Bananas" => 10, "Mangoes" => 45];

if[array_key_exists["Apples", $values]] {
    echo 'We have apples.';
}
// We have apples.
72 có thể được sử dụng với 
$values = ["Apples", "Bananas", "Mangoes", "100", "200"];

if[in_array[100, $values]] {
    echo '100 is one of the values';
}
// 100 is one of the values

if[in_array[200, $values] !== false] {
    echo '200 is not one of the values';
}
// 200 is not one of the values

$values = ["Apples", "Bananas", "Mangoes", "100", "200", 100];

echo array_search[100, $values];
// 3

echo array_search[100, $values, true];
// 5

$values = ["Apples" => 100, "Bananas" => 10, "Mangoes" => 45];

if[array_key_exists["Apples", $values]] {
    echo 'We have apples.';
}
// We have apples.
77, điều này làm cho cấu trúc này thậm chí còn tốt hơn

$first = array[10, "Apple", 20, -18, "Monkey"];
print_r[$first];
/* Array
[
    [0] => 10
    [1] => Apple
    [2] => 20
    [3] => -18
    [4] => Monkey
] */

$second = [10, "Apple", 20, -18, "Monkey"];
print_r[$second];
/* Array
[
    [0] => 10
    [1] => Apple
    [2] => 20
    [3] => -18
    [4] => Monkey
] */

$third = [10, 5 => "Apple", 2 => 20, -18, "Monkey"];
print_r[$third];
/* Array
[
    [0] => 10
    [5] => Apple
    [2] => 20
    [6] => -18
    [7] => Monkey
] */
5

Với hàm 

$values = ["Apples", "Bananas", "Mangoes", "100", "200"];

if[in_array[100, $values]] {
    echo '100 is one of the values';
}
// 100 is one of the values

if[in_array[200, $values] !== false] {
    echo '200 is not one of the values';
}
// 200 is not one of the values

$values = ["Apples", "Bananas", "Mangoes", "100", "200", 100];

echo array_search[100, $values];
// 3

echo array_search[100, $values, true];
// 5

$values = ["Apples" => 100, "Bananas" => 10, "Mangoes" => 45];

if[array_key_exists["Apples", $values]] {
    echo 'We have apples.';
}
// We have apples.
78, bạn có thể xuất một mảng kết hợp thành các biến. Đối với mỗi phần tử của một mảng, một biến sẽ được tạo với tên của khóa và giá trị là giá trị của phần tử

$first = array[10, "Apple", 20, -18, "Monkey"];
print_r[$first];
/* Array
[
    [0] => 10
    [1] => Apple
    [2] => 20
    [3] => -18
    [4] => Monkey
] */

$second = [10, "Apple", 20, -18, "Monkey"];
print_r[$second];
/* Array
[
    [0] => 10
    [1] => Apple
    [2] => 20
    [3] => -18
    [4] => Monkey
] */

$third = [10, 5 => "Apple", 2 => 20, -18, "Monkey"];
print_r[$third];
/* Array
[
    [0] => 10
    [5] => Apple
    [2] => 20
    [6] => -18
    [7] => Monkey
] */
7

Xin lưu ý rằng 

$values = ["Apples", "Bananas", "Mangoes", "100", "200"];

if[in_array[100, $values]] {
    echo '100 is one of the values';
}
// 100 is one of the values

if[in_array[200, $values] !== false] {
    echo '200 is not one of the values';
}
// 200 is not one of the values

$values = ["Apples", "Bananas", "Mangoes", "100", "200", 100];

echo array_search[100, $values];
// 3

echo array_search[100, $values, true];
// 5

$values = ["Apples" => 100, "Bananas" => 10, "Mangoes" => 45];

if[array_key_exists["Apples", $values]] {
    echo 'We have apples.';
}
// We have apples.
78 không an toàn nếu bạn đang làm việc với dữ liệu người dùng [chẳng hạn như kết quả của các yêu cầu], vì vậy, tốt hơn là sử dụng chức năng này với các cờ 
$first = array[10, "Apple", 20, -18, "Monkey"];
print_r[$first];
/* Array
[
    [0] => 10
    [1] => Apple
    [2] => 20
    [3] => -18
    [4] => Monkey
] */

$second = [10, "Apple", 20, -18, "Monkey"];
print_r[$second];
/* Array
[
    [0] => 10
    [1] => Apple
    [2] => 20
    [3] => -18
    [4] => Monkey
] */

$third = [10, 5 => "Apple", 2 => 20, -18, "Monkey"];
print_r[$third];
/* Array
[
    [0] => 10
    [5] => Apple
    [2] => 20
    [6] => -18
    [7] => Monkey
] */
20 và 
$first = array[10, "Apple", 20, -18, "Monkey"];
print_r[$first];
/* Array
[
    [0] => 10
    [1] => Apple
    [2] => 20
    [3] => -18
    [4] => Monkey
] */

$second = [10, "Apple", 20, -18, "Monkey"];
print_r[$second];
/* Array
[
    [0] => 10
    [1] => Apple
    [2] => 20
    [3] => -18
    [4] => Monkey
] */

$third = [10, 5 => "Apple", 2 => 20, -18, "Monkey"];
print_r[$third];
/* Array
[
    [0] => 10
    [5] => Apple
    [2] => 20
    [6] => -18
    [7] => Monkey
] */
21

Ngược lại với hàm trước đó là hàm 

$first = array[10, "Apple", 20, -18, "Monkey"];
print_r[$first];
/* Array
[
    [0] => 10
    [1] => Apple
    [2] => 20
    [3] => -18
    [4] => Monkey
] */

$second = [10, "Apple", 20, -18, "Monkey"];
print_r[$second];
/* Array
[
    [0] => 10
    [1] => Apple
    [2] => 20
    [3] => -18
    [4] => Monkey
] */

$third = [10, 5 => "Apple", 2 => 20, -18, "Monkey"];
print_r[$third];
/* Array
[
    [0] => 10
    [5] => Apple
    [2] => 20
    [6] => -18
    [7] => Monkey
] */
22, tạo một mảng kết hợp từ các biến

print_r[array_keys[$array]]; // ['sky', 'grass', 'orange']
print_r[array_values[$array]]; // ['blue', 'green', 'orange']
print_r[array_flip[$array]];

// Array
// [
//     [blue] => sky
//     [green] => grass
//     [orange] => orange
// ]
2

Chức năng lọc

Có một chức năng tuyệt vời để lọc mảng và nó được gọi là 

$first = array[10, "Apple", 20, -18, "Monkey"];
print_r[$first];
/* Array
[
    [0] => 10
    [1] => Apple
    [2] => 20
    [3] => -18
    [4] => Monkey
] */

$second = [10, "Apple", 20, -18, "Monkey"];
print_r[$second];
/* Array
[
    [0] => 10
    [1] => Apple
    [2] => 20
    [3] => -18
    [4] => Monkey
] */

$third = [10, 5 => "Apple", 2 => 20, -18, "Monkey"];
print_r[$third];
/* Array
[
    [0] => 10
    [5] => Apple
    [2] => 20
    [6] => -18
    [7] => Monkey
] */
23. Truyền mảng dưới dạng tham số đầu tiên và hàm ẩn danh làm tham số thứ hai. Trả lại 
$first = array[10, "Apple", 20, -18, "Monkey"];
print_r[$first];
/* Array
[
    [0] => 10
    [1] => Apple
    [2] => 20
    [3] => -18
    [4] => Monkey
] */

$second = [10, "Apple", 20, -18, "Monkey"];
print_r[$second];
/* Array
[
    [0] => 10
    [1] => Apple
    [2] => 20
    [3] => -18
    [4] => Monkey
] */

$third = [10, 5 => "Apple", 2 => 20, -18, "Monkey"];
print_r[$third];
/* Array
[
    [0] => 10
    [5] => Apple
    [2] => 20
    [6] => -18
    [7] => Monkey
] */
24 trong hàm gọi lại nếu bạn muốn để nguyên phần tử này trong mảng và 
$first = array[10, "Apple", 20, -18, "Monkey"];
print_r[$first];
/* Array
[
    [0] => 10
    [1] => Apple
    [2] => 20
    [3] => -18
    [4] => Monkey
] */

$second = [10, "Apple", 20, -18, "Monkey"];
print_r[$second];
/* Array
[
    [0] => 10
    [1] => Apple
    [2] => 20
    [3] => -18
    [4] => Monkey
] */

$third = [10, 5 => "Apple", 2 => 20, -18, "Monkey"];
print_r[$third];
/* Array
[
    [0] => 10
    [5] => Apple
    [2] => 20
    [6] => -18
    [7] => Monkey
] */
25 nếu bạn không muốn

print_r[array_keys[$array]]; // ['sky', 'grass', 'orange']
print_r[array_values[$array]]; // ['blue', 'green', 'orange']
print_r[array_flip[$array]];

// Array
// [
//     [blue] => sky
//     [green] => grass
//     [orange] => orange
// ]
6

Có một cách để lọc không chỉ theo các giá trị. Bạn có thể sử dụng 

$first = array[10, "Apple", 20, -18, "Monkey"];
print_r[$first];
/* Array
[
    [0] => 10
    [1] => Apple
    [2] => 20
    [3] => -18
    [4] => Monkey
] */

$second = [10, "Apple", 20, -18, "Monkey"];
print_r[$second];
/* Array
[
    [0] => 10
    [1] => Apple
    [2] => 20
    [3] => -18
    [4] => Monkey
] */

$third = [10, 5 => "Apple", 2 => 20, -18, "Monkey"];
print_r[$third];
/* Array
[
    [0] => 10
    [5] => Apple
    [2] => 20
    [6] => -18
    [7] => Monkey
] */
26 hoặc 
$first = array[10, "Apple", 20, -18, "Monkey"];
print_r[$first];
/* Array
[
    [0] => 10
    [1] => Apple
    [2] => 20
    [3] => -18
    [4] => Monkey
] */

$second = [10, "Apple", 20, -18, "Monkey"];
print_r[$second];
/* Array
[
    [0] => 10
    [1] => Apple
    [2] => 20
    [3] => -18
    [4] => Monkey
] */

$third = [10, 5 => "Apple", 2 => 20, -18, "Monkey"];
print_r[$third];
/* Array
[
    [0] => 10
    [5] => Apple
    [2] => 20
    [6] => -18
    [7] => Monkey
] */
27 làm tham số thứ ba để chuyển khóa hoặc cả giá trị và khóa cho hàm gọi lại

Ngoài ra, bạn có thể gọi 

$first = array[10, "Apple", 20, -18, "Monkey"];
print_r[$first];
/* Array
[
    [0] => 10
    [1] => Apple
    [2] => 20
    [3] => -18
    [4] => Monkey
] */

$second = [10, "Apple", 20, -18, "Monkey"];
print_r[$second];
/* Array
[
    [0] => 10
    [1] => Apple
    [2] => 20
    [3] => -18
    [4] => Monkey
] */

$third = [10, 5 => "Apple", 2 => 20, -18, "Monkey"];
print_r[$third];
/* Array
[
    [0] => 10
    [5] => Apple
    [2] => 20
    [6] => -18
    [7] => Monkey
] */
23 mà không cần gọi lại để xóa tất cả các giá trị trống

$keys = ['sky', 'grass', 'orange'];
$values = ['blue', 'green', 'orange'];

$array = array_combine[$keys, $values];
print_r[$array];

// Array
// [
//     [sky] => blue
//     [grass] => green
//     [orange] => orange
// ]
0

Bạn chỉ có thể nhận các giá trị duy nhất từ ​​một mảng bằng cách sử dụng hàm 

$first = array[10, "Apple", 20, -18, "Monkey"];
print_r[$first];
/* Array
[
    [0] => 10
    [1] => Apple
    [2] => 20
    [3] => -18
    [4] => Monkey
] */

$second = [10, "Apple", 20, -18, "Monkey"];
print_r[$second];
/* Array
[
    [0] => 10
    [1] => Apple
    [2] => 20
    [3] => -18
    [4] => Monkey
] */

$third = [10, 5 => "Apple", 2 => 20, -18, "Monkey"];
print_r[$third];
/* Array
[
    [0] => 10
    [5] => Apple
    [2] => 20
    [6] => -18
    [7] => Monkey
] */
29. Lưu ý rằng hàm sẽ bảo toàn các khóa của các phần tử duy nhất đầu tiên

$keys = ['sky', 'grass', 'orange'];
$values = ['blue', 'green', 'orange'];

$array = array_combine[$keys, $values];
print_r[$array];

// Array
// [
//     [sky] => blue
//     [grass] => green
//     [orange] => orange
// ]
1

Với 

$first = array[10, "Apple", 20, -18, "Monkey"];
print_r[$first];
/* Array
[
    [0] => 10
    [1] => Apple
    [2] => 20
    [3] => -18
    [4] => Monkey
] */

$second = [10, "Apple", 20, -18, "Monkey"];
print_r[$second];
/* Array
[
    [0] => 10
    [1] => Apple
    [2] => 20
    [3] => -18
    [4] => Monkey
] */

$third = [10, 5 => "Apple", 2 => 20, -18, "Monkey"];
print_r[$third];
/* Array
[
    [0] => 10
    [5] => Apple
    [2] => 20
    [6] => -18
    [7] => Monkey
] */
50, bạn có thể nhận danh sách các giá trị cột từ một mảng nhiều chiều, chẳng hạn như câu trả lời từ cơ sở dữ liệu SQL hoặc bản nhập từ tệp CSV. Chỉ cần chuyển một mảng và tên cột

$keys = ['sky', 'grass', 'orange'];
$values = ['blue', 'green', 'orange'];

$array = array_combine[$keys, $values];
print_r[$array];

// Array
// [
//     [sky] => blue
//     [grass] => green
//     [orange] => orange
// ]
2

Bắt đầu từ PHP 7, 

$first = array[10, "Apple", 20, -18, "Monkey"];
print_r[$first];
/* Array
[
    [0] => 10
    [1] => Apple
    [2] => 20
    [3] => -18
    [4] => Monkey
] */

$second = [10, "Apple", 20, -18, "Monkey"];
print_r[$second];
/* Array
[
    [0] => 10
    [1] => Apple
    [2] => 20
    [3] => -18
    [4] => Monkey
] */

$third = [10, 5 => "Apple", 2 => 20, -18, "Monkey"];
print_r[$third];
/* Array
[
    [0] => 10
    [5] => Apple
    [2] => 20
    [6] => -18
    [7] => Monkey
] */
50 thậm chí còn trở nên mạnh mẽ hơn vì giờ đây nó được phép. Vì vậy, làm việc với một loạt các mô hình trở nên dễ dàng hơn

$keys = ['sky', 'grass', 'orange'];
$values = ['blue', 'green', 'orange'];

$array = array_combine[$keys, $values];
print_r[$array];

// Array
// [
//     [sky] => blue
//     [grass] => green
//     [orange] => orange
// ]
3

Đi bộ qua các mảng

Sử dụng 

$first = array[10, "Apple", 20, -18, "Monkey"];
print_r[$first];
/* Array
[
    [0] => 10
    [1] => Apple
    [2] => 20
    [3] => -18
    [4] => Monkey
] */

$second = [10, "Apple", 20, -18, "Monkey"];
print_r[$second];
/* Array
[
    [0] => 10
    [1] => Apple
    [2] => 20
    [3] => -18
    [4] => Monkey
] */

$third = [10, 5 => "Apple", 2 => 20, -18, "Monkey"];
print_r[$third];
/* Array
[
    [0] => 10
    [5] => Apple
    [2] => 20
    [6] => -18
    [7] => Monkey
] */
52, bạn có thể áp dụng lệnh gọi lại cho mọi phần tử của một mảng. Bạn có thể chuyển tên hàm hoặc hàm ẩn danh để nhận một mảng mới dựa trên mảng đã cho

$keys = ['sky', 'grass', 'orange'];
$values = ['blue', 'green', 'orange'];

$array = array_combine[$keys, $values];
print_r[$array];

// Array
// [
//     [sky] => blue
//     [grass] => green
//     [orange] => orange
// ]
4

Có một huyền thoại rằng không có cách nào để chuyển các giá trị và khóa của một mảng thành một cuộc gọi lại, nhưng chúng ta có thể phá vỡ nó

$keys = ['sky', 'grass', 'orange'];
$values = ['blue', 'green', 'orange'];

$array = array_combine[$keys, $values];
print_r[$array];

// Array
// [
//     [sky] => blue
//     [grass] => green
//     [orange] => orange
// ]
5

Nhưng điều này có vẻ bẩn. Tốt hơn nên sử dụng 

$first = array[10, "Apple", 20, -18, "Monkey"];
print_r[$first];
/* Array
[
    [0] => 10
    [1] => Apple
    [2] => 20
    [3] => -18
    [4] => Monkey
] */

$second = [10, "Apple", 20, -18, "Monkey"];
print_r[$second];
/* Array
[
    [0] => 10
    [1] => Apple
    [2] => 20
    [3] => -18
    [4] => Monkey
] */

$third = [10, 5 => "Apple", 2 => 20, -18, "Monkey"];
print_r[$third];
/* Array
[
    [0] => 10
    [5] => Apple
    [2] => 20
    [6] => -18
    [7] => Monkey
] */
53 để thay thế. Chức năng này trông giống như 
$first = array[10, "Apple", 20, -18, "Monkey"];
print_r[$first];
/* Array
[
    [0] => 10
    [1] => Apple
    [2] => 20
    [3] => -18
    [4] => Monkey
] */

$second = [10, "Apple", 20, -18, "Monkey"];
print_r[$second];
/* Array
[
    [0] => 10
    [1] => Apple
    [2] => 20
    [3] => -18
    [4] => Monkey
] */

$third = [10, 5 => "Apple", 2 => 20, -18, "Monkey"];
print_r[$third];
/* Array
[
    [0] => 10
    [5] => Apple
    [2] => 20
    [6] => -18
    [7] => Monkey
] */
52, nhưng nó hoạt động khác. Trước hết, một mảng được truyền bằng cách sử dụng một tham chiếu, vì vậy 
$first = array[10, "Apple", 20, -18, "Monkey"];
print_r[$first];
/* Array
[
    [0] => 10
    [1] => Apple
    [2] => 20
    [3] => -18
    [4] => Monkey
] */

$second = [10, "Apple", 20, -18, "Monkey"];
print_r[$second];
/* Array
[
    [0] => 10
    [1] => Apple
    [2] => 20
    [3] => -18
    [4] => Monkey
] */

$third = [10, 5 => "Apple", 2 => 20, -18, "Monkey"];
print_r[$third];
/* Array
[
    [0] => 10
    [5] => Apple
    [2] => 20
    [6] => -18
    [7] => Monkey
] */
53 không tạo một mảng mới mà thay đổi một mảng đã cho. Vì vậy, với tư cách là một mảng nguồn, bạn có thể chuyển giá trị mảng bằng tham chiếu trong hàm gọi lại. Các phím mảng cũng có thể được chuyển dễ dàng

$keys = ['sky', 'grass', 'orange'];
$values = ['blue', 'green', 'orange'];

$array = array_combine[$keys, $values];
print_r[$array];

// Array
// [
//     [sky] => blue
//     [grass] => green
//     [orange] => orange
// ]
6

Tham gia mảng

Cách tốt nhất để hợp nhất hai hoặc nhiều mảng trong PHP là sử dụng hàm 

$first = array[10, "Apple", 20, -18, "Monkey"];
print_r[$first];
/* Array
[
    [0] => 10
    [1] => Apple
    [2] => 20
    [3] => -18
    [4] => Monkey
] */

$second = [10, "Apple", 20, -18, "Monkey"];
print_r[$second];
/* Array
[
    [0] => 10
    [1] => Apple
    [2] => 20
    [3] => -18
    [4] => Monkey
] */

$third = [10, 5 => "Apple", 2 => 20, -18, "Monkey"];
print_r[$third];
/* Array
[
    [0] => 10
    [5] => Apple
    [2] => 20
    [6] => -18
    [7] => Monkey
] */
56 . Các mục của mảng sẽ được hợp nhất với nhau và các giá trị có cùng khóa chuỗi sẽ được ghi đè bằng giá trị cuối cùng

$keys = ['sky', 'grass', 'orange'];
$values = ['blue', 'green', 'orange'];

$array = array_combine[$keys, $values];
print_r[$array];

// Array
// [
//     [sky] => blue
//     [grass] => green
//     [orange] => orange
// ]
7

Để xóa các giá trị mảng khỏi một mảng [hoặc nhiều mảng] khác, hãy sử dụng 

$first = array[10, "Apple", 20, -18, "Monkey"];
print_r[$first];
/* Array
[
    [0] => 10
    [1] => Apple
    [2] => 20
    [3] => -18
    [4] => Monkey
] */

$second = [10, "Apple", 20, -18, "Monkey"];
print_r[$second];
/* Array
[
    [0] => 10
    [1] => Apple
    [2] => 20
    [3] => -18
    [4] => Monkey
] */

$third = [10, 5 => "Apple", 2 => 20, -18, "Monkey"];
print_r[$third];
/* Array
[
    [0] => 10
    [5] => Apple
    [2] => 20
    [6] => -18
    [7] => Monkey
] */
57. Để nhận các giá trị có trong các mảng nhất định, hãy sử dụng 
$first = array[10, "Apple", 20, -18, "Monkey"];
print_r[$first];
/* Array
[
    [0] => 10
    [1] => Apple
    [2] => 20
    [3] => -18
    [4] => Monkey
] */

$second = [10, "Apple", 20, -18, "Monkey"];
print_r[$second];
/* Array
[
    [0] => 10
    [1] => Apple
    [2] => 20
    [3] => -18
    [4] => Monkey
] */

$third = [10, 5 => "Apple", 2 => 20, -18, "Monkey"];
print_r[$third];
/* Array
[
    [0] => 10
    [5] => Apple
    [2] => 20
    [6] => -18
    [7] => Monkey
] */
58. Các ví dụ tiếp theo sẽ cho thấy nó hoạt động như thế nào

$keys = ['sky', 'grass', 'orange'];
$values = ['blue', 'green', 'orange'];

$array = array_combine[$keys, $values];
print_r[$array];

// Array
// [
//     [sky] => blue
//     [grass] => green
//     [orange] => orange
// ]
8

Làm phép toán với các giá trị mảng

Sử dụng 

$first = array[10, "Apple", 20, -18, "Monkey"];
print_r[$first];
/* Array
[
    [0] => 10
    [1] => Apple
    [2] => 20
    [3] => -18
    [4] => Monkey
] */

$second = [10, "Apple", 20, -18, "Monkey"];
print_r[$second];
/* Array
[
    [0] => 10
    [1] => Apple
    [2] => 20
    [3] => -18
    [4] => Monkey
] */

$third = [10, 5 => "Apple", 2 => 20, -18, "Monkey"];
print_r[$third];
/* Array
[
    [0] => 10
    [5] => Apple
    [2] => 20
    [6] => -18
    [7] => Monkey
] */
59 để tính tổng các giá trị mảng, 
$first = array[10, "Apple", 20, -18, "Monkey"];
print_r[$first];
/* Array
[
    [0] => 10
    [1] => Apple
    [2] => 20
    [3] => -18
    [4] => Monkey
] */

$second = [10, "Apple", 20, -18, "Monkey"];
print_r[$second];
/* Array
[
    [0] => 10
    [1] => Apple
    [2] => 20
    [3] => -18
    [4] => Monkey
] */

$third = [10, 5 => "Apple", 2 => 20, -18, "Monkey"];
print_r[$third];
/* Array
[
    [0] => 10
    [5] => Apple
    [2] => 20
    [6] => -18
    [7] => Monkey
] */
70 để nhân chúng hoặc tạo công thức của riêng bạn với 
$first = array[10, "Apple", 20, -18, "Monkey"];
print_r[$first];
/* Array
[
    [0] => 10
    [1] => Apple
    [2] => 20
    [3] => -18
    [4] => Monkey
] */

$second = [10, "Apple", 20, -18, "Monkey"];
print_r[$second];
/* Array
[
    [0] => 10
    [1] => Apple
    [2] => 20
    [3] => -18
    [4] => Monkey
] */

$third = [10, 5 => "Apple", 2 => 20, -18, "Monkey"];
print_r[$third];
/* Array
[
    [0] => 10
    [5] => Apple
    [2] => 20
    [6] => -18
    [7] => Monkey
] */
71

$keys = ['sky', 'grass', 'orange'];
$values = ['blue', 'green', 'orange'];

$array = array_combine[$keys, $values];
print_r[$array];

// Array
// [
//     [sky] => blue
//     [grass] => green
//     [orange] => orange
// ]
9

Để đếm tất cả các giá trị của một mảng, hãy sử dụng 

$first = array[10, "Apple", 20, -18, "Monkey"];
print_r[$first];
/* Array
[
    [0] => 10
    [1] => Apple
    [2] => 20
    [3] => -18
    [4] => Monkey
] */

$second = [10, "Apple", 20, -18, "Monkey"];
print_r[$second];
/* Array
[
    [0] => 10
    [1] => Apple
    [2] => 20
    [3] => -18
    [4] => Monkey
] */

$third = [10, 5 => "Apple", 2 => 20, -18, "Monkey"];
print_r[$third];
/* Array
[
    [0] => 10
    [5] => Apple
    [2] => 20
    [6] => -18
    [7] => Monkey
] */
72. Nó sẽ cung cấp tất cả các giá trị duy nhất của một mảng nhất định dưới dạng khóa và số lượng các giá trị này dưới dạng giá trị

print_r[array_keys[$array]]; // ['sky', 'grass', 'orange']
print_r[array_values[$array]]; // ['blue', 'green', 'orange']
print_r[array_flip[$array]];

// Array
// [
//     [blue] => sky
//     [green] => grass
//     [orange] => orange
// ]
0

Tạo mảng

Để tạo một mảng có kích thước nhất định và cùng một giá trị, hãy sử dụng 

$first = array[10, "Apple", 20, -18, "Monkey"];
print_r[$first];
/* Array
[
    [0] => 10
    [1] => Apple
    [2] => 20
    [3] => -18
    [4] => Monkey
] */

$second = [10, "Apple", 20, -18, "Monkey"];
print_r[$second];
/* Array
[
    [0] => 10
    [1] => Apple
    [2] => 20
    [3] => -18
    [4] => Monkey
] */

$third = [10, 5 => "Apple", 2 => 20, -18, "Monkey"];
print_r[$third];
/* Array
[
    [0] => 10
    [5] => Apple
    [2] => 20
    [6] => -18
    [7] => Monkey
] */
73

print_r[array_keys[$array]]; // ['sky', 'grass', 'orange']
print_r[array_values[$array]]; // ['blue', 'green', 'orange']
print_r[array_flip[$array]];

// Array
// [
//     [blue] => sky
//     [green] => grass
//     [orange] => orange
// ]
1

Để tạo một mảng có nhiều khóa và giá trị, chẳng hạn như giờ trong ngày hoặc chữ cái, hãy sử dụng range[]

print_r[array_keys[$array]]; // ['sky', 'grass', 'orange']
print_r[array_values[$array]]; // ['blue', 'green', 'orange']
print_r[array_flip[$array]];

// Array
// [
//     [blue] => sky
//     [green] => grass
//     [orange] => orange
// ]
2

Để lấy một phần của mảng—ví dụ: chỉ ba phần tử đầu tiên—hãy sử dụng 

$first = array[10, "Apple", 20, -18, "Monkey"];
print_r[$first];
/* Array
[
    [0] => 10
    [1] => Apple
    [2] => 20
    [3] => -18
    [4] => Monkey
] */

$second = [10, "Apple", 20, -18, "Monkey"];
print_r[$second];
/* Array
[
    [0] => 10
    [1] => Apple
    [2] => 20
    [3] => -18
    [4] => Monkey
] */

$third = [10, 5 => "Apple", 2 => 20, -18, "Monkey"];
print_r[$third];
/* Array
[
    [0] => 10
    [5] => Apple
    [2] => 20
    [6] => -18
    [7] => Monkey
] */
74

print_r[array_keys[$array]]; // ['sky', 'grass', 'orange']
print_r[array_values[$array]]; // ['blue', 'green', 'orange']
print_r[array_flip[$array]];

// Array
// [
//     [blue] => sky
//     [green] => grass
//     [orange] => orange
// ]
3

Nếu bạn muốn tạo một mảng kết hợp với các khóa khác nhau và cùng một giá trị được gán cho mỗi khóa, bạn chỉ cần sử dụng hàm 

$first = array[10, "Apple", 20, -18, "Monkey"];
print_r[$first];
/* Array
[
    [0] => 10
    [1] => Apple
    [2] => 20
    [3] => -18
    [4] => Monkey
] */

$second = [10, "Apple", 20, -18, "Monkey"];
print_r[$second];
/* Array
[
    [0] => 10
    [1] => Apple
    [2] => 20
    [3] => -18
    [4] => Monkey
] */

$third = [10, 5 => "Apple", 2 => 20, -18, "Monkey"];
print_r[$third];
/* Array
[
    [0] => 10
    [5] => Apple
    [2] => 20
    [6] => -18
    [7] => Monkey
] */
75

print_r[array_keys[$array]]; // ['sky', 'grass', 'orange']
print_r[array_values[$array]]; // ['blue', 'green', 'orange']
print_r[array_flip[$array]];

// Array
// [
//     [blue] => sky
//     [green] => grass
//     [orange] => orange
// ]
4

Sắp xếp mảng

Bạn nên nhớ rằng mọi hàm sắp xếp trong PHP đều hoạt động với các mảng theo một tham chiếu và trả về true khi thành công hoặc false khi thất bại. Có một hàm sắp xếp cơ bản có tên là 

$first = array[10, "Apple", 20, -18, "Monkey"];
print_r[$first];
/* Array
[
    [0] => 10
    [1] => Apple
    [2] => 20
    [3] => -18
    [4] => Monkey
] */

$second = [10, "Apple", 20, -18, "Monkey"];
print_r[$second];
/* Array
[
    [0] => 10
    [1] => Apple
    [2] => 20
    [3] => -18
    [4] => Monkey
] */

$third = [10, 5 => "Apple", 2 => 20, -18, "Monkey"];
print_r[$third];
/* Array
[
    [0] => 10
    [5] => Apple
    [2] => 20
    [6] => -18
    [7] => Monkey
] */
76 và hàm này sắp xếp các giá trị theo thứ tự tăng dần mà không giữ nguyên khóa. Chức năng sắp xếp có thể được thêm vào trước bởi các chữ cái sau

  • a, sắp xếp khóa bảo toàn
  • k, sắp xếp theo phím
  • r, sắp xếp theo thứ tự đảo ngược/giảm dần
  • u, sắp xếp với chức năng người dùng

Bạn có thể thấy sự kết hợp của các chữ cái này trong bảng sau


akrua
$first = array[10, "Apple", 20, -18, "Monkey"];
print_r[$first];
/* Array
[
    [0] => 10
    [1] => Apple
    [2] => 20
    [3] => -18
    [4] => Monkey
] */

$second = [10, "Apple", 20, -18, "Monkey"];
print_r[$second];
/* Array
[
    [0] => 10
    [1] => Apple
    [2] => 20
    [3] => -18
    [4] => Monkey
] */

$third = [10, 5 => "Apple", 2 => 20, -18, "Monkey"];
print_r[$third];
/* Array
[
    [0] => 10
    [5] => Apple
    [2] => 20
    [6] => -18
    [7] => Monkey
] */
77
$first = array[10, "Apple", 20, -18, "Monkey"];
print_r[$first];
/* Array
[
    [0] => 10
    [1] => Apple
    [2] => 20
    [3] => -18
    [4] => Monkey
] */

$second = [10, "Apple", 20, -18, "Monkey"];
print_r[$second];
/* Array
[
    [0] => 10
    [1] => Apple
    [2] => 20
    [3] => -18
    [4] => Monkey
] */

$third = [10, 5 => "Apple", 2 => 20, -18, "Monkey"];
print_r[$third];
/* Array
[
    [0] => 10
    [5] => Apple
    [2] => 20
    [6] => -18
    [7] => Monkey
] */
78
$first = array[10, "Apple", 20, -18, "Monkey"];
print_r[$first];
/* Array
[
    [0] => 10
    [1] => Apple
    [2] => 20
    [3] => -18
    [4] => Monkey
] */

$second = [10, "Apple", 20, -18, "Monkey"];
print_r[$second];
/* Array
[
    [0] => 10
    [1] => Apple
    [2] => 20
    [3] => -18
    [4] => Monkey
] */

$third = [10, 5 => "Apple", 2 => 20, -18, "Monkey"];
print_r[$third];
/* Array
[
    [0] => 10
    [5] => Apple
    [2] => 20
    [6] => -18
    [7] => Monkey
] */
79k
print_r[array_keys[$array]]; // ['sky', 'grass', 'orange']
print_r[array_values[$array]]; // ['blue', 'green', 'orange']
print_r[array_flip[$array]];

// Array
// [
//     [blue] => sky
//     [green] => grass
//     [orange] => orange
// ]
20
print_r[array_keys[$array]]; // ['sky', 'grass', 'orange']
print_r[array_values[$array]]; // ['blue', 'green', 'orange']
print_r[array_flip[$array]];

// Array
// [
//     [blue] => sky
//     [green] => grass
//     [orange] => orange
// ]
21
r
$first = array[10, "Apple", 20, -18, "Monkey"];
print_r[$first];
/* Array
[
    [0] => 10
    [1] => Apple
    [2] => 20
    [3] => -18
    [4] => Monkey
] */

$second = [10, "Apple", 20, -18, "Monkey"];
print_r[$second];
/* Array
[
    [0] => 10
    [1] => Apple
    [2] => 20
    [3] => -18
    [4] => Monkey
] */

$third = [10, 5 => "Apple", 2 => 20, -18, "Monkey"];
print_r[$third];
/* Array
[
    [0] => 10
    [5] => Apple
    [2] => 20
    [6] => -18
    [7] => Monkey
] */
78
print_r[array_keys[$array]]; // ['sky', 'grass', 'orange']
print_r[array_values[$array]]; // ['blue', 'green', 'orange']
print_r[array_flip[$array]];

// Array
// [
//     [blue] => sky
//     [green] => grass
//     [orange] => orange
// ]
21
print_r[array_keys[$array]]; // ['sky', 'grass', 'orange']
print_r[array_values[$array]]; // ['blue', 'green', 'orange']
print_r[array_flip[$array]];

// Array
// [
//     [blue] => sky
//     [green] => grass
//     [orange] => orange
// ]
24
u
$first = array[10, "Apple", 20, -18, "Monkey"];
print_r[$first];
/* Array
[
    [0] => 10
    [1] => Apple
    [2] => 20
    [3] => -18
    [4] => Monkey
] */

$second = [10, "Apple", 20, -18, "Monkey"];
print_r[$second];
/* Array
[
    [0] => 10
    [1] => Apple
    [2] => 20
    [3] => -18
    [4] => Monkey
] */

$third = [10, 5 => "Apple", 2 => 20, -18, "Monkey"];
print_r[$third];
/* Array
[
    [0] => 10
    [5] => Apple
    [2] => 20
    [6] => -18
    [7] => Monkey
] */
79

print_r[array_keys[$array]]; // ['sky', 'grass', 'orange']
print_r[array_values[$array]]; // ['blue', 'green', 'orange']
print_r[array_flip[$array]];

// Array
// [
//     [blue] => sky
//     [green] => grass
//     [orange] => orange
// ]
26

Kết hợp các hàm mảng như một ông chủ

Điều kỳ diệu thực sự bắt đầu khi bạn bắt đầu kết hợp các hàm mảng. Sau đây là cách bạn có thể cắt và xóa các giá trị trống chỉ trong một dòng mã với 

$first = array[10, "Apple", 20, -18, "Monkey"];
print_r[$first];
/* Array
[
    [0] => 10
    [1] => Apple
    [2] => 20
    [3] => -18
    [4] => Monkey
] */

$second = [10, "Apple", 20, -18, "Monkey"];
print_r[$second];
/* Array
[
    [0] => 10
    [1] => Apple
    [2] => 20
    [3] => -18
    [4] => Monkey
] */

$third = [10, 5 => "Apple", 2 => 20, -18, "Monkey"];
print_r[$third];
/* Array
[
    [0] => 10
    [5] => Apple
    [2] => 20
    [6] => -18
    [7] => Monkey
] */
23 và 
$first = array[10, "Apple", 20, -18, "Monkey"];
print_r[$first];
/* Array
[
    [0] => 10
    [1] => Apple
    [2] => 20
    [3] => -18
    [4] => Monkey
] */

$second = [10, "Apple", 20, -18, "Monkey"];
print_r[$second];
/* Array
[
    [0] => 10
    [1] => Apple
    [2] => 20
    [3] => -18
    [4] => Monkey
] */

$third = [10, 5 => "Apple", 2 => 20, -18, "Monkey"];
print_r[$third];
/* Array
[
    [0] => 10
    [5] => Apple
    [2] => 20
    [6] => -18
    [7] => Monkey
] */
52

print_r[array_keys[$array]]; // ['sky', 'grass', 'orange']
print_r[array_values[$array]]; // ['blue', 'green', 'orange']
print_r[array_flip[$array]];

// Array
// [
//     [blue] => sky
//     [green] => grass
//     [orange] => orange
// ]
5

Để tạo id cho bản đồ tiêu đề từ một loạt các mô hình, chúng ta có thể sử dụng kết hợp 

$values = ["Apples", "Bananas", "Mangoes", "100", "200"];

if[in_array[100, $values]] {
    echo '100 is one of the values';
}
// 100 is one of the values

if[in_array[200, $values] !== false] {
    echo '200 is not one of the values';
}
// 200 is not one of the values

$values = ["Apples", "Bananas", "Mangoes", "100", "200", 100];

echo array_search[100, $values];
// 3

echo array_search[100, $values, true];
// 5

$values = ["Apples" => 100, "Bananas" => 10, "Mangoes" => 45];

if[array_key_exists["Apples", $values]] {
    echo 'We have apples.';
}
// We have apples.
3 và 
$first = array[10, "Apple", 20, -18, "Monkey"];
print_r[$first];
/* Array
[
    [0] => 10
    [1] => Apple
    [2] => 20
    [3] => -18
    [4] => Monkey
] */

$second = [10, "Apple", 20, -18, "Monkey"];
print_r[$second];
/* Array
[
    [0] => 10
    [1] => Apple
    [2] => 20
    [3] => -18
    [4] => Monkey
] */

$third = [10, 5 => "Apple", 2 => 20, -18, "Monkey"];
print_r[$third];
/* Array
[
    [0] => 10
    [5] => Apple
    [2] => 20
    [6] => -18
    [7] => Monkey
] */
50

print_r[array_keys[$array]]; // ['sky', 'grass', 'orange']
print_r[array_values[$array]]; // ['blue', 'green', 'orange']
print_r[array_flip[$array]];

// Array
// [
//     [blue] => sky
//     [green] => grass
//     [orange] => orange
// ]
6

Để lấy ba giá trị trên cùng của một mảng, chúng ta có thể sử dụng 

$first = array[10, "Apple", 20, -18, "Monkey"];
print_r[$first];
/* Array
[
    [0] => 10
    [1] => Apple
    [2] => 20
    [3] => -18
    [4] => Monkey
] */

$second = [10, "Apple", 20, -18, "Monkey"];
print_r[$second];
/* Array
[
    [0] => 10
    [1] => Apple
    [2] => 20
    [3] => -18
    [4] => Monkey
] */

$third = [10, 5 => "Apple", 2 => 20, -18, "Monkey"];
print_r[$third];
/* Array
[
    [0] => 10
    [5] => Apple
    [2] => 20
    [6] => -18
    [7] => Monkey
] */
72, 
$first = array[10, "Apple", 20, -18, "Monkey"];
print_r[$first];
/* Array
[
    [0] => 10
    [1] => Apple
    [2] => 20
    [3] => -18
    [4] => Monkey
] */

$second = [10, "Apple", 20, -18, "Monkey"];
print_r[$second];
/* Array
[
    [0] => 10
    [1] => Apple
    [2] => 20
    [3] => -18
    [4] => Monkey
] */

$third = [10, 5 => "Apple", 2 => 20, -18, "Monkey"];
print_r[$third];
/* Array
[
    [0] => 10
    [5] => Apple
    [2] => 20
    [6] => -18
    [7] => Monkey
] */
78 và 
$first = array[10, "Apple", 20, -18, "Monkey"];
print_r[$first];
/* Array
[
    [0] => 10
    [1] => Apple
    [2] => 20
    [3] => -18
    [4] => Monkey
] */

$second = [10, "Apple", 20, -18, "Monkey"];
print_r[$second];
/* Array
[
    [0] => 10
    [1] => Apple
    [2] => 20
    [3] => -18
    [4] => Monkey
] */

$third = [10, 5 => "Apple", 2 => 20, -18, "Monkey"];
print_r[$third];
/* Array
[
    [0] => 10
    [5] => Apple
    [2] => 20
    [6] => -18
    [7] => Monkey
] */
74

print_r[array_keys[$array]]; // ['sky', 'grass', 'orange']
print_r[array_values[$array]]; // ['blue', 'green', 'orange']
print_r[array_flip[$array]];

// Array
// [
//     [blue] => sky
//     [green] => grass
//     [orange] => orange
// ]
7

Thật dễ dàng để sử dụng 

$first = array[10, "Apple", 20, -18, "Monkey"];
print_r[$first];
/* Array
[
    [0] => 10
    [1] => Apple
    [2] => 20
    [3] => -18
    [4] => Monkey
] */

$second = [10, "Apple", 20, -18, "Monkey"];
print_r[$second];
/* Array
[
    [0] => 10
    [1] => Apple
    [2] => 20
    [3] => -18
    [4] => Monkey
] */

$third = [10, 5 => "Apple", 2 => 20, -18, "Monkey"];
print_r[$third];
/* Array
[
    [0] => 10
    [5] => Apple
    [2] => 20
    [6] => -18
    [7] => Monkey
] */
59 và 
$first = array[10, "Apple", 20, -18, "Monkey"];
print_r[$first];
/* Array
[
    [0] => 10
    [1] => Apple
    [2] => 20
    [3] => -18
    [4] => Monkey
] */

$second = [10, "Apple", 20, -18, "Monkey"];
print_r[$second];
/* Array
[
    [0] => 10
    [1] => Apple
    [2] => 20
    [3] => -18
    [4] => Monkey
] */

$third = [10, 5 => "Apple", 2 => 20, -18, "Monkey"];
print_r[$third];
/* Array
[
    [0] => 10
    [5] => Apple
    [2] => 20
    [6] => -18
    [7] => Monkey
] */
52 để tính tổng thứ tự trong một vài hàng

print_r[array_keys[$array]]; // ['sky', 'grass', 'orange']
print_r[array_values[$array]]; // ['blue', 'green', 'orange']
print_r[array_flip[$array]];

// Array
// [
//     [blue] => sky
//     [green] => grass
//     [orange] => orange
// ]
8

Sự kết luận

Như bạn có thể thấy, kiến ​​thức về các hàm mảng chính có thể làm cho mã của bạn ngắn hơn và dễ đọc hơn. Tất nhiên, PHP có nhiều hàm mảng hơn và thậm chí các hàm đã cho cũng có nhiều biến thể để sử dụng với các tham số và cờ bổ sung, nhưng tôi nghĩ rằng trong hướng dẫn này, chúng ta đã đề cập đến những điều cơ bản mà mọi nhà phát triển PHP nên biết

Học PHP với một khóa học trực tuyến miễn phí

Nếu bạn muốn học PHP, hãy xem khóa học trực tuyến miễn phí của chúng tôi về các nguyên tắc cơ bản của PHP

Trong khóa học này, bạn sẽ học các nguyên tắc cơ bản của lập trình PHP. Bạn sẽ bắt đầu với những kiến ​​thức cơ bản, học cách PHP hoạt động và viết các hàm và vòng lặp PHP đơn giản. Sau đó, bạn sẽ xây dựng các lớp mã hóa để lập trình hướng đối tượng đơn giản [OOP]. Đồng thời, bạn sẽ học tất cả các kỹ năng quan trọng nhất để viết ứng dụng cho web. bạn sẽ có cơ hội thực hành phản hồi các yêu cầu GET và POST, phân tích cú pháp JSON, xác thực người dùng và sử dụng cơ sở dữ liệu MySQL

Làm cách nào để lưu trữ nhiều giá trị cho một khóa trong mảng PHP?

Biến nó thành một mảng. mảng['key' => mảng[1,2,3]] – Michael Berkowski. Tháng hai 14, 2014 tại 18. 02
@MichaelBerkowski trong trường hợp này làm thế nào tôi có thể nhận được giá trị của khóa? . -/ – AliM. .
Xem lại mảng PHP Ví dụ $array['key'][1] có giá trị 2. Và $array['key'] có giá trị mảng array[1,2,3]

Khóa có thể có nhiều giá trị PHP không?

PHP không hỗ trợ trả về nhiều giá trị trong một hàm .

Một mảng PHP có thể có các khóa trùng lặp không?

Kiểm tra mã. Nhân đôi khóa mảng . Nếu nhiều phần tử trong khai báo mảng sử dụng cùng một khóa thì chỉ phần tử cuối cùng sẽ được sử dụng và tất cả các phần tử khác sẽ bị ghi đè. Reports duplicate keys in array declarations. If multiple elements in the array declaration use the same key, only the last one will be used, and all others will be overwritten.

Làm cách nào để lưu trữ nhiều giá trị trong khóa trong PHP?

Để lưu trữ nhiều giá trị, có hai cách thực hiện tác vụ. Một cách là gán từng giá trị cho một biến duy nhất và cách còn lại, hiệu quả hơn nhiều, là gán nhiều giá trị cho một biến. Đó là những gì chúng ta gọi là một mảng. Một mảng là một cách để lưu trữ nhiều giá trị trong một biến duy nhất.

Chủ Đề