Hướng dẫn how do you check array is empty or not php? - làm thế nào để bạn kiểm tra mảng trống hay không php?

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 empty

Mảng hai: $ mảngtwo không trống
$ArrayTwo is not empty

Mả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 empty

Mảng hai: $ mảngtwo không trống
$arraytwo is not empty

Mả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()

$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.

18 năm trước

mcfogw tại gmail dot com ¶Determine whether a variable is empty

6 năm trước

Thomas tại Thomasnoest dot nl ¶(mixed

$ArrayFour = array();
4): bool

e dot klerks tại i-byte dot nl ¶

fahimcseiiuc tại gmail dot com ¶

Greg Hartwig ¶

Ellisgl ¶empty() is essentially the concise equivalent to !isset($var) || $var == false.

15 năm trước

Jmarbas tại hotmail dot com

$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
.

17 năm trước

Rodolphe Dot Bodeau tại miễn phí dot fr ¶empty() / isset() comparison.

$ArrayFour = array();
9

Qeremy ¶empty() 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
  }
0

rkulla2 tại gmail dot com

bool(true)
bool(false)
bool(false)
bool(false)
bool(true)
bool(true)

Lukas ¶

6 tháng trước: Because this is a language construct and not a function, it cannot be called using variable functions, or named arguments.

Gazettesdf ¶:

4 năm trướcempty() on inaccessible object properties, the __isset() overloading method will be called, if declared.

Javier Alfonso ¶

  • Xzero tại Elite7hackers Dot Net
  • __isset()
  • AdityCse tại Gmail Dot Com ¶
  • Làm thế nào kiểm tra dữ liệu trống hoặc không trong PHP?
  • Hàm php trống () hàm trống () kiểm tra xem một biến có trống hay không. Hàm này trả về sai nếu biến tồn tại và không trống, nếu không nó sẽ trả về đúng.
  • Mảng trống có trống PHP không?
  • Một mảng trống là giả trong PHP, vì vậy bạn thậm chí không cần phải sử dụng trống () như những người khác đã đề xuất. PHP trống () của PHP xác định xem một biến không tồn tại hay có giá trị giả (như mảng (), 0, null, false, v.v.).

Mảng trống có đúng trong PHP không?

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
  }
4

Janci ¶

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
  }
8

Steven 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
  }
9

bool(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
  }
8

Thô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
  }
8

Chrisdmiddleton tại Gmail Dot Com ¶

8 năm trước

empty()8

empty()9

name="array[]"0

wranvaud tại gmail dot com ¶

5 năm trước

name="array[]"1

Claudio Galdiolo ¶

5 năm trước

name="array[]"2

Claudio Galdiolo ¶

11 năm trước

name="array[]"3

phpsort ¶

Denobocation-bozic et yahoo.com

name="array[]"4

name="array[]"5

name="array[]"6

name="array[]"7

13 năm trước

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
  }
8

Chrisdmiddleton tại Gmail Dot Com ¶

8 năm trước

empty()1

wranvaud tại gmail dot com ¶

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
  }
8

5 năm trước

8 năm trước

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
  }
8

wranvaud tại gmail dot com ¶

Denobocation-bozic et yahoo.com

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
  }
8

13 năm trước

8 năm trước

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
  }
8

wranvaud tại gmail dot com ¶

5 năm trước

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
  }
8

Claudio Galdiolo ¶

phpsort ¶

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
  }
8

Denobocation-bozic et yahoo.com

Denobocation-bozic et yahoo.com

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
  }
8

13 năm trước

8 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
  }
8

wranvaud tại gmail dot com ¶

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
  }
8

Chrisdmiddleton tại Gmail Dot Com ¶

8 năm trước

$ArrayFour = array();
01

wranvaud tại gmail dot com ¶

5 năm trước

$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
  }
8

Claudio Galdiolo ¶

8 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
  }
8

wranvaud tại gmail dot com ¶

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 ¶

8 năm trước

$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
  }
8

wranvaud tại gmail dot com ¶

5 năm trước

$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();
23

Claudio Galdiolo ¶

phpsort ¶

$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
  }
8

Denobocation-bozic et yahoo.com

phpsort ¶

$ArrayFour = array();
28

$ArrayFour = array();
29

$ArrayFour = array();
30

$ArrayFour = array();
31

$ArrayFour = array();
32

Denobocation-bozic et yahoo.com

5 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
  }
8

Claudio Galdiolo ¶

5 năm trước

$ArrayFour = array();
36

$ArrayFour = array();
37

$ArrayFour = array();
38

Làm thế nào kiểm tra dữ liệu trống hoặc không trong PHP?

Hàm php trống () hàm trống () kiểm tra xem một biến có trống hay không.Hàm này trả về sai nếu biến tồn tại và không trống, nếu không nó sẽ trả về đúng.empty() Function The empty() function checks whether a variable is empty or not. This function returns false if the variable exists and is not empty, otherwise it returns true.

Mảng trống có trống PHP không?

Một mảng trống là giả trong PHP, vì vậy bạn thậm chí không cần phải sử dụng trống () như những người khác đã đề xuất.PHP trống () của PHP xác định xem một biến không tồn tại hay có giá trị giả (như mảng (), 0, null, false, v.v.)., so you don't even need to use empty() as others have suggested. PHP's empty() determines if a variable doesn't exist or has a falsey value (like array() , 0 , null , false , etc).

Mảng trống có đúng trong PHP không?

Một mảng trống đánh giá là sai, bất kỳ mảng nào khác thành true (demo): , any other array to TRUE (Demo):

Làm thế nào kiểm tra mảng trống hoặc không trong php laravel?

Laravel - Kiểm tra độ trống của một mảng trong Blade @if (trống ($ sản phẩm))..
Chỉ số chức năng công cộng ().
$ sản phẩm = sản phẩm :: get () ;;.
Quay trở lại ('Trang chủ', Compact ('Sản phẩm')) ;.