How to Find the Number of Empty Elements in a PHP Array

104 16
    • 1). Open a text editor, such as Notepad, and create a new file. Write a line of code that defines an array using PHP's "array" function. The syntax for the function is "array(value1, value2, ...);." Here is an example:

      $myArray = array('Jack', '', null, 17.6);

    • 2). Write a line of code that executes PHP's "array_filter" function with the array from Step 1 as the parameter and saves the result into a new variable. Here is an example:

      $filteredArray = array_filter($myArray);

    • 3). Write a line of code that subtracts the number of elements in the array from Step 1, which is found using PHP's "count" function, from the number of elements in the array from Step 2 and saves it into a new variable. The new variable now contains the number of empty elements in the original array.

      $emptyCount = count($myArray)-count($filteredArray);

Subscribe to our newsletter
Sign up here to get the latest news, updates and special offers delivered directly to your inbox.
You can unsubscribe at any time

Leave A Reply

Your email address will not be published.