Một số câu trả lời tốt, nhưng chỉ nghĩ rằng tôi sẽ mở rộng một chút để giải thích rõ ràng hơn khi PHP xác định xem một mảng có trống không.
Ghi chú chính:
Một mảng có khóa [hoặc khóa] sẽ được xác định là không trống bởi PHP.
Vì các giá trị mảng cần các khóa tồn tại, có các giá trị hoặc không trong một mảng không xác định xem nó có trống hay không, chỉ khi không có khóa [và do đó không có giá trị].
Vì vậy, kiểm tra một mảng với empty[]
không chỉ đơn giản cho bạn biết bạn có giá trị hay không, nó cho bạn biết nếu mảng trống và các phím là một phần của mảng.
Vì vậy, hãy xem xét cách bạn sản xuất mảng của bạn trước khi quyết định sử dụng phương pháp kiểm tra nào. Ví dụ, một mảng sẽ có các khóa khi người dùng gửi biểu mẫu HTML của bạn khi mỗi trường biểu mẫu có tên mảng [tức là name="array[]"
]. Một mảng không trống sẽ được sản xuất cho mỗi trường vì sẽ có các giá trị khóa được tăng tự động cho mỗi mảng của trường mẫu.
EG An array will have keys when a user submits your HTML form when each form field has an array name [ie name="array[]"
].
A non empty array will be produced for each field as there will be auto incremented key values for each form field's array.
Lấy các mảng này làm ví dụ:
/* Assigning some arrays */
// Array with user defined key and value
$ArrayOne = array["UserKeyA" => "UserValueA", "UserKeyB" => "UserValueB"];
// Array with auto increment key and user defined value
// as a form field would return with user input
$ArrayTwo[] = "UserValue01";
$ArrayTwo[] = "UserValue02";
// Array with auto incremented key and no value
// as a form field would return without user input
$ArrayThree[] = '';
$ArrayThree[] = '';
Nếu bạn lặp lại các phím và giá trị mảng cho các mảng trên, bạn sẽ nhận được những điều sau:
Mảng một: [userKeya] => [uservaluea] [userKeyB] => [uservalueb]
[UserKeyA] => [UserValueA]
[UserKeyB] => [UserValueB]Mảng hai: [0] => [uservalue01] [1] => [uservalue02]
[0] => [UserValue01]
[1] => [UserValue02]Mảng ba: [0] => [] [1] => [] []
[0] => []
[1] => []
Và kiểm tra các mảng trên với empty[]
trả về các kết quả sau:
Mảng một: $ mảng không trống
$ArrayOne is not emptyMảng hai: $ mảngtwo không trống
$ArrayTwo is not emptyMảng ba: $ mảngthree không trống
$ArrayThree is not empty
Một mảng sẽ luôn trống khi bạn gán một mảng nhưng không sử dụng nó sau đó, chẳng hạn như:
$ArrayFour = array[];
Điều này sẽ trống, IE PHP sẽ trả về true khi sử dụng nếu empty[]
ở trên.
Vì vậy, nếu mảng của bạn có các khóa - bằng ví dụ: tên đầu vào của biểu mẫu hoặc nếu bạn gán chúng theo cách thủ công [tức là tạo một mảng có tên cột cơ sở dữ liệu là các khóa nhưng không có giá trị/dữ liệu nào từ cơ sở dữ liệu], thì mảng sẽ không phải là empty[]
.
Trong trường hợp này, bạn có thể lặp lại mảng trong một foreach, kiểm tra xem mỗi khóa có giá trị không. Đây là một phương pháp tốt nếu bạn cần chạy qua mảng, có thể kiểm tra các phím hoặc dữ liệu vệ sinh.
Tuy nhiên, đó không phải là phương pháp tốt nhất nếu bạn chỉ cần biết "nếu các giá trị tồn tại" trả về đúng hoặc sai. Có nhiều phương pháp khác nhau để xác định xem một mảng có bất kỳ giá trị nào khi biết nó sẽ có khóa hay không. Một chức năng hoặc lớp học có thể là cách tiếp cận tốt nhất, nhưng như mọi khi, nó phụ thuộc vào môi trường của bạn và các yêu cầu chính xác, cũng như những thứ khác như những gì bạn hiện đang làm với mảng [nếu có].
Đây là một cách tiếp cận sử dụng rất ít mã để kiểm tra xem một mảng có giá trị không:
Sử dụng array_filter[]
: Lặp lại trên mỗi giá trị trong mảng chuyển chúng đến hàm gọi lại. Nếu hàm gọi lại trả về true, giá trị hiện tại từ mảng được trả lại vào mảng kết quả. Các phím mảng được bảo tồn.
Iterates over each value in the array passing them to the callback function. If the callback function returns true, the current value from array is returned into the result array. Array keys are preserved.
$EmptyTestArray = array_filter[$ArrayOne];
if [!empty[$EmptyTestArray]]
{
// do some tests on the values in $ArrayOne
}
else
{
// Likely not to need an else,
// but could return message to user "you entered nothing" etc etc
}
Chạy array_filter[]
trên cả ba mảng ví dụ [được tạo trong khối mã đầu tiên trong câu trả lời này] kết quả sau:
Mảng một: $ mảng không trống
$arrayone is not emptyMảng hai: $ mảngtwo không trống
$arraytwo is not emptyMảng ba: $ mảngthree trống
$arraythree is empty
Vì vậy, khi không có giá trị, dù có các khóa hay không, sử dụng array_filter[]
để tạo một mảng mới và sau đó kiểm tra xem mảng mới có trống hiển thị nếu có bất kỳ giá trị nào trong mảng gốc. Nó không lý tưởng và hơi lộn xộn, nhưng nếu bạn có một mảng lớn và không cần phải vượt qua nó vì bất kỳ lý do nào khác, thì đây là đơn giản nhất về mã cần thiết.
It is not ideal and a bit messy, but if you have a huge
array and don't need to loop through it for any other reason, then this is the simplest in terms of code needed.
Tôi không có kinh nghiệm trong việc kiểm tra chi phí, nhưng sẽ rất tốt khi biết sự khác biệt giữa việc sử dụng kiểm tra array_filter[]
và
$ArrayFour = array[];
3 nếu tìm thấy một giá trị.Rõ ràng là điểm chuẩn sẽ cần phải có trên các tham số khác nhau, trên các mảng nhỏ và lớn và khi có các giá trị và không, v.v.
[Php 4, Php 5, Php 7, Php 8]
trống - xác định xem một biến có trống không — Determine whether a variable is empty
Sự mô tả
trống rỗng [hỗn hợp
4]: bool[mixed $ArrayFour = array[];
4]: bool$ArrayFour = array[];
Thông số
$ArrayFour = array[];
5Biến cần được kiểm tra
Không có cảnh báo nào được tạo ra nếu biến không tồn tại. Điều đó có nghĩa là trống [] về cơ bản là tương đương ngắn gọn với! ISSET [$ var] || $ var == Sai.empty[] is essentially the concise equivalent to !isset[$var] || $var == false.
Trả về giá trị
Trả về
$ArrayFour = array[];
6 Nếu $ArrayFour = array[];
5 không tồn tại hoặc có giá trị trống hoặc bằng 0, hay còn gọi là Fisey, xem chuyển đổi sang Boolean. Nếu không thì trả về $ArrayFour = array[];
8.$ArrayFour = array[];
6 if $ArrayFour = array[];
5 does not exist or has a value that is empty or
equal to zero, aka falsey, see conversion to boolean. Otherwise returns $ArrayFour = array[];
8. Ví dụ
Ví dụ #1 Một so sánh đơn giản trống [] / isset [].empty[] / isset[] comparison.
$ArrayFour = array[];
9Ví dụ #2 trống [] trên chuỗi offsetsempty[] on String Offsets
$EmptyTestArray = array_filter[$ArrayOne];
if [!empty[$EmptyTestArray]]
{
// do some tests on the values in $ArrayOne
}
else
{
// Likely not to need an else,
// but could return message to user "you entered nothing" etc etc
}
0Ví dụ trên sẽ xuất ra:
bool[true] bool[false] bool[false] bool[false] bool[true] bool[true]
Ghi chú
Lưu ý: Bởi vì đây là một cấu trúc ngôn ngữ và không phải là một hàm, nó không thể được gọi là sử dụng các hàm biến hoặc các đối số được đặt tên.: Because this is a language construct and not a function, it cannot be called using variable functions, or named arguments.
Ghi chú::
Khi sử dụng trống [] trên các thuộc tính đối tượng không thể truy cập, phương thức quá tải __isset [] sẽ được gọi, nếu được khai báo.empty[] on inaccessible object properties, the __isset[] overloading method will be called, if declared.
Xem thêm
- ISSET [] - Xác định xem một biến được khai báo và khác với NULL
- __isset[]
- unset [] - Und đặt một biến đã cho
- Array_Key_Exists [] - Kiểm tra xem khóa hoặc chỉ mục đã cho có tồn tại trong mảng
- Count [] - Đếm tất cả các phần tử trong một mảng hoặc trong một đối tượng có thể đếm được
- strlen [] - Nhận độ dài chuỗi
- Các bảng so sánh loại
Nanhe Kumar ¶ ¶
8 năm trước
$EmptyTestArray = array_filter[$ArrayOne];
if [!empty[$EmptyTestArray]]
{
// do some tests on the values in $ArrayOne
}
else
{
// Likely not to need an else,
// but could return message to user "you entered nothing" etc etc
}
1$EmptyTestArray = array_filter[$ArrayOne];
if [!empty[$EmptyTestArray]]
{
// do some tests on the values in $ArrayOne
}
else
{
// Likely not to need an else,
// but could return message to user "you entered nothing" etc etc
}
2$EmptyTestArray = array_filter[$ArrayOne];
if [!empty[$EmptyTestArray]]
{
// do some tests on the values in $ArrayOne
}
else
{
// Likely not to need an else,
// but could return message to user "you entered nothing" etc etc
}
3$EmptyTestArray = array_filter[$ArrayOne];
if [!empty[$EmptyTestArray]]
{
// do some tests on the values in $ArrayOne
}
else
{
// Likely not to need an else,
// but could return message to user "you entered nothing" etc etc
}
4Janci ¶ ¶
13 năm trước
$EmptyTestArray = array_filter[$ArrayOne];
if [!empty[$EmptyTestArray]]
{
// do some tests on the values in $ArrayOne
}
else
{
// Likely not to need an else,
// but could return message to user "you entered nothing" etc etc
}
5$EmptyTestArray = array_filter[$ArrayOne];
if [!empty[$EmptyTestArray]]
{
// do some tests on the values in $ArrayOne
}
else
{
// Likely not to need an else,
// but could return message to user "you entered nothing" etc etc
}
6$EmptyTestArray = array_filter[$ArrayOne];
if [!empty[$EmptyTestArray]]
{
// do some tests on the values in $ArrayOne
}
else
{
// Likely not to need an else,
// but could return message to user "you entered nothing" etc etc
}
7$EmptyTestArray = array_filter[$ArrayOne];
if [!empty[$EmptyTestArray]]
{
// do some tests on the values in $ArrayOne
}
else
{
// Likely not to need an else,
// but could return message to user "you entered nothing" etc etc
}
8Steven tại Nevvix Dot Com ¶ ¶
11 năm trước
$EmptyTestArray = array_filter[$ArrayOne];
if [!empty[$EmptyTestArray]]
{
// do some tests on the values in $ArrayOne
}
else
{
// Likely not to need an else,
// but could return message to user "you entered nothing" etc etc
}
9bool[true] bool[false] bool[false] bool[false] bool[true] bool[true]0
$EmptyTestArray = array_filter[$ArrayOne];
if [!empty[$EmptyTestArray]]
{
// do some tests on the values in $ArrayOne
}
else
{
// Likely not to need an else,
// but could return message to user "you entered nothing" etc etc
}
8Thông tin tại Ensostudio Dot Ru ¶ ¶
1 năm trước
bool[true] bool[false] bool[false] bool[false] bool[true] bool[true]2
Markmanning tại Gmail Dot Com ¶ ¶
3 năm trước
bool[true] bool[false] bool[false] bool[false] bool[true] bool[true]3
bool[true] bool[false] bool[false] bool[false] bool[true] bool[true]4
bool[true] bool[false] bool[false] bool[false] bool[true] bool[true]5
bool[true] bool[false] bool[false] bool[false] bool[true] bool[true]6
anh em chấm của bạn dot t tại hotmail dot com ¶
7 năm trước
bool[true] bool[false] bool[false] bool[false] bool[true] bool[true]7
bool[true] bool[false] bool[false] bool[false] bool[true] bool[true]8
bool[true] bool[false] bool[false] bool[false] bool[true] bool[true]9
Martin Dot Aarhof tại Gmail Dot Com ¶ ¶
10 năm trước
empty[]
0
empty[]
1
$EmptyTestArray = array_filter[$ArrayOne];
if [!empty[$EmptyTestArray]]
{
// do some tests on the values in $ArrayOne
}
else
{
// Likely not to need an else,
// but could return message to user "you entered nothing" etc etc
}
8Ẩn danh ¶ ¶
14 năm trước
empty[]
3
empty[]
4
empty[]
5
empty[]
6
$EmptyTestArray = array_filter[$ArrayOne];
if [!empty[$EmptyTestArray]]
{
// do some tests on the values in $ArrayOne
}
else
{
// Likely not to need an else,
// but could return message to user "you entered nothing" etc etc
}
8Chrisdmiddleton tại Gmail Dot Com ¶ ¶
8 năm trước
empty[]
8
empty[]
9
name="array[]"
0Janci ¶ ¶
13 năm trước
name="array[]"
1
Steven tại Nevvix Dot Com ¶ ¶
13 năm trước
name="array[]"
2
Steven tại Nevvix Dot Com ¶ ¶
11 năm trước
name="array[]"
3
Thông tin tại Ensostudio Dot Ru ¶ ¶
13 năm trước
name="array[]"
4
name="array[]"
5
name="array[]"
6
name="array[]"
7Steven tại Nevvix Dot Com ¶ ¶
14 năm trước
name="array[]"
8
name="array[]"
9
$EmptyTestArray = array_filter[$ArrayOne];
if [!empty[$EmptyTestArray]]
{
// do some tests on the values in $ArrayOne
}
else
{
// Likely not to need an else,
// but could return message to user "you entered nothing" etc etc
}
8Chrisdmiddleton tại Gmail Dot Com ¶ ¶
wranvaud tại gmail dot com ¶
empty[]
1
5 năm trước ¶
11 năm trước
empty[]
2
empty[]
3
empty[]
4
empty[]
5
$EmptyTestArray = array_filter[$ArrayOne];
if [!empty[$EmptyTestArray]]
{
// do some tests on the values in $ArrayOne
}
else
{
// Likely not to need an else,
// but could return message to user "you entered nothing" etc etc
}
8Thông tin tại Ensostudio Dot Ru ¶ ¶
wranvaud tại gmail dot com ¶
empty[]
7
empty[]
8
empty[]
9
$EmptyTestArray = array_filter[$ArrayOne];
if [!empty[$EmptyTestArray]]
{
// do some tests on the values in $ArrayOne
}
else
{
// Likely not to need an else,
// but could return message to user "you entered nothing" etc etc
}
85 năm trước ¶
13 năm trước
empty[]
1
empty[]
2
$EmptyTestArray = array_filter[$ArrayOne];
if [!empty[$EmptyTestArray]]
{
// do some tests on the values in $ArrayOne
}
else
{
// Likely not to need an else,
// but could return message to user "you entered nothing" etc etc
}
8Steven tại Nevvix Dot Com ¶ ¶
wranvaud tại gmail dot com ¶
empty[]
4
empty[]
5
empty[]
6
empty[]
7
$EmptyTestArray = array_filter[$ArrayOne];
if [!empty[$EmptyTestArray]]
{
// do some tests on the values in $ArrayOne
}
else
{
// Likely not to need an else,
// but could return message to user "you entered nothing" etc etc
}
85 năm trước ¶
Claudio Galdiolo ¶
empty[]
9
empty[]
0
empty[]
1
$EmptyTestArray = array_filter[$ArrayOne];
if [!empty[$EmptyTestArray]]
{
// do some tests on the values in $ArrayOne
}
else
{
// Likely not to need an else,
// but could return message to user "you entered nothing" etc etc
}
8phpsort ¶ ¶
Denobocation-bozic et yahoo.com
empty[]
3
empty[]
4
empty[]
5
empty[]
6
empty[]
7
$EmptyTestArray = array_filter[$ArrayOne];
if [!empty[$EmptyTestArray]]
{
// do some tests on the values in $ArrayOne
}
else
{
// Likely not to need an else,
// but could return message to user "you entered nothing" etc etc
}
8Tom tại Tomwardrop Dot Com ¶ ¶
13 năm trước
empty[]
9
array_filter[]
0
$EmptyTestArray = array_filter[$ArrayOne];
if [!empty[$EmptyTestArray]]
{
// do some tests on the values in $ArrayOne
}
else
{
// Likely not to need an else,
// but could return message to user "you entered nothing" etc etc
}
8Steven tại Nevvix Dot Com ¶ ¶
11 năm trước
array_filter[]
2
array_filter[]
3
$EmptyTestArray = array_filter[$ArrayOne];
if [!empty[$EmptyTestArray]]
{
// do some tests on the values in $ArrayOne
}
else
{
// Likely not to need an else,
// but could return message to user "you entered nothing" etc etc
}
8Thông tin tại Ensostudio Dot Ru ¶ ¶
3 năm trước
array_filter[]
5
anh em chấm của bạn dot t tại hotmail dot com ¶
14 năm trước
array_filter[]
6
array_filter[]
7
array_filter[]
8
array_filter[]
9
$EmptyTestArray = array_filter[$ArrayOne];
if [!empty[$EmptyTestArray]]
{
// do some tests on the values in $ArrayOne
}
else
{
// Likely not to need an else,
// but could return message to user "you entered nothing" etc etc
}
8Chrisdmiddleton tại Gmail Dot Com ¶ ¶
wranvaud tại gmail dot com ¶
$ArrayFour = array[];
015 năm trước ¶
Claudio Galdiolo ¶
$ArrayFour = array[];
02$ArrayFour = array[];
03$ArrayFour = array[];
04$ArrayFour = array[];
05$ArrayFour = array[];
06$EmptyTestArray = array_filter[$ArrayOne];
if [!empty[$EmptyTestArray]]
{
// do some tests on the values in $ArrayOne
}
else
{
// Likely not to need an else,
// but could return message to user "you entered nothing" etc etc
}
8phpsort ¶ ¶
11 năm trước
$ArrayFour = array[];
08$ArrayFour = array[];
09$EmptyTestArray = array_filter[$ArrayOne];
if [!empty[$EmptyTestArray]]
{
// do some tests on the values in $ArrayOne
}
else
{
// Likely not to need an else,
// but could return message to user "you entered nothing" etc etc
}
8Thông tin tại Ensostudio Dot Ru ¶ ¶
10 năm trước
$ArrayFour = array[];
11$ArrayFour = array[];
12$EmptyTestArray = array_filter[$ArrayOne];
if [!empty[$EmptyTestArray]]
{
// do some tests on the values in $ArrayOne
}
else
{
// Likely not to need an else,
// but could return message to user "you entered nothing" etc etc
}
8Ẩn danh ¶ ¶
wranvaud tại gmail dot com ¶
$ArrayFour = array[];
14$ArrayFour = array[];
15$ArrayFour = array[];
16$ArrayFour = array[];
17$ArrayFour = array[];
18$ArrayFour = array[];
19$EmptyTestArray = array_filter[$ArrayOne];
if [!empty[$EmptyTestArray]]
{
// do some tests on the values in $ArrayOne
}
else
{
// Likely not to need an else,
// but could return message to user "you entered nothing" etc etc
}
85 năm trước ¶
Claudio Galdiolo ¶
$ArrayFour = array[];
21$EmptyTestArray = array_filter[$ArrayOne];
if [!empty[$EmptyTestArray]]
{
// do some tests on the values in $ArrayOne
}
else
{
// Likely not to need an else,
// but could return message to user "you entered nothing" etc etc
}
3$ArrayFour = array[];
23phpsort ¶ ¶
Denobocation-bozic et yahoo.com
$ArrayFour = array[];
24$ArrayFour = array[];
25$ArrayFour = array[];
26$EmptyTestArray = array_filter[$ArrayOne];
if [!empty[$EmptyTestArray]]
{
// do some tests on the values in $ArrayOne
}
else
{
// Likely not to need an else,
// but could return message to user "you entered nothing" etc etc
}
8Tom tại Tomwardrop Dot Com ¶ ¶
Denobocation-bozic et yahoo.com
$ArrayFour = array[];
28$ArrayFour = array[];
29$ArrayFour = array[];
30$ArrayFour = array[];
31$ArrayFour = array[];
32Tom tại Tomwardrop Dot Com ¶ ¶
13 năm trước
$ArrayFour = array[];
33$ArrayFour = array[];
34$EmptyTestArray = array_filter[$ArrayOne];
if [!empty[$EmptyTestArray]]
{
// do some tests on the values in $ArrayOne
}
else
{
// Likely not to need an else,
// but could return message to user "you entered nothing" etc etc
}
8Steven tại Nevvix Dot Com ¶ ¶
Denobocation-bozic et yahoo.com
$ArrayFour = array[];
36$ArrayFour = array[];
37$ArrayFour = array[];
38