Hướng dẫn is empty array in php? - là mảng trống trong 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

$ArrayFour = array();
1: 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

$ArrayFour = array();
1 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

$ArrayFour = array();
1 để 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

$ArrayFour = array();
1 và
$ArrayFour = array();
5 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.

Là điều kiện trố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. Các giá trị sau đánh giá là trống: 0.

Làm thế nào để bạn kiểm tra nếu một mảng trống?

  • Để kiểm tra xem một mảng có trống hay không, bạn có thể sử dụng thuộc tính .length. Thuộc tính độ dài đặt hoặc trả về số lượng phần tử trong một mảng. Bằng cách biết số lượng các phần tử trong mảng, bạn có thể biết nó có trống hay không.
  • Mảng trống có đúng không?
  • Là điều kiện trố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. Các giá trị sau đánh giá là trống: 0.

    Làm thế nào để bạn kiểm tra nếu một mảng trống?

    Để kiểm tra xem một mảng có trống hay không, bạn có thể sử dụng thuộc tính .length. Thuộc tính độ dài đặt hoặc trả về số lượng phần tử trong một mảng. Bằng cách biết số lượng các phần tử trong mảng, bạn có thể biết nó có trống hay không.

    1. Sử dụng hàm trống (): Hàm này xác định xem một biến nhất định có trống hay không. Hàm này không trả về cảnh báo nếu một biến không tồn tại. This function determines whether a given variable is empty. This function does not return a warning if a variable does not exist.

      Syntax:

      bool empty( $var )

      Example:

      $ArrayFour = array();
      
      6

      $ArrayFour = array();
      
      7
      $ArrayFour = array();
      
      8
      $ArrayFour = 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
        }
      
      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
        }
      
      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
      $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
      $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 empty( $var )
      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
        }
      
      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
        }
      
      4
      $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
      $ArrayFour = array();
      
      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
        }
      
      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
      bool empty( $var )
      9
      bool empty( $var )
      0

      Given Array is not empty 
      Given Array is empty
      1

      Output:

      Given Array is not empty 
      Given Array is empty

    2. Sử dụng chức năng đếm: Hàm này đếm tất cả các phần tử trong một mảng. Nếu số lượng phần tử trong mảng bằng 0, thì nó sẽ hiển thị mảng trống. This function counts all the elements in an array. If number of elements in array is zero, then it will display empty array.

      Syntax:

      int count( $array_or_countable )

      Example:

      $ArrayFour = array();
      
      6

      $ArrayFour = array();
      
      7
      $ArrayFour = array();
      
      8
      $ArrayFour = 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
        }
      
      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
        }
      
      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
        }
      
      4
      Given Array is not empty 
      Given Array is 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
        }
      
      4
      $ArrayFour = array();
      
      7
      int count( $array_or_countable )
      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
        }
      
      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
      $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 empty( $var )
      0

      int count( $array_or_countable )
      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
        }
      
      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
      bool empty( $var )
      9
      bool empty( $var )
      0

      Given Array is not empty 
      Given Array is empty
      1

    3. Sử dụng chức năng đếm: Hàm này đếm tất cả các phần tử trong một mảng. Nếu số lượng phần tử trong mảng bằng 0, thì nó sẽ hiển thị mảng trống. This method check the size of array. If the size of array is zero then array is empty otherwise array is not empty.

      Example:

      $ArrayFour = array();
      
      6

      $ArrayFour = array();
      
      7
      $ArrayFour = array();
      
      8
      $ArrayFour = 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
        }
      
      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
        }
      
      1empty()9
      $ArrayFour = array();
      
      7name="array[]"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
        }
      
      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
      $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 empty( $var )
      0

      int count( $array_or_countable )
      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
        }
      
      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
      bool empty( $var )
      9
      bool empty( $var )
      0

      Given Array is not empty 
      Given Array is empty
      1

    Sử dụng chức năng đếm: Hàm này đếm tất cả các phần tử trong một mảng. Nếu số lượng phần tử trong mảng bằng 0, thì nó sẽ hiển thị mảng trống.


    Mảng trống có 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).

    Là điều kiện trố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.Các giá trị sau đánh giá là trống: 0.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. The following values evaluates to empty: 0.

    Làm thế nào để bạn kiểm tra nếu một mảng trống?

    Để kiểm tra xem một mảng có trống hay không, bạn có thể sử dụng thuộc tính .length.Thuộc tính độ dài đặt hoặc trả về số lượng phần tử trong một mảng.Bằng cách biết số lượng các phần tử trong mảng, bạn có thể biết nó có trống hay không.use the . length property. The length property sets or returns the number of elements in an array. By knowing the number of elements in the array, you can tell if it is empty or not.

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

    Nếu độ dài của đối tượng là 0, thì mảng được coi là trống và hàm sẽ trả về đúng.Nếu không thì mảng không trống và hàm sẽ trả về sai. and the function will return TRUE. Else the array is not empty and the function will return False.