Check if Array is Empty in PHP

Use empty() function

To check if an array is empty in PHP, you can use the empty() function. This function accepts an array as its argument, and it returns true if the array is empty and false if the array is not empty.

Here is an example of how to use the empty() function to check if an array is empty:

Output

Use count() function

To check if an array is empty in PHP, you can also use the count() function. This function accepts an array as its argument, and it returns the number of elements in the array. To check if an array is empty using the count() function, you can check if the number of elements in the array is equal to 0.

Here is an example of how to use the count() function to check if an array is empty:

Output

Use array_key_exists() function

Another approach to check if an array is empty in PHP is to use the array_key_exists() function.

This function accepts an array and a key as its arguments, and it returns true if the specified key exists in the array and false if the specified key does not exist in the array.

Here is an example of how to use the array_key_exists() function to check if an array is empty:

Output

Use array_filter() function

One more approach to check if an array is empty in PHP is to use the array_filter() function.

This function accepts an array and a callback function as its arguments, and it returns an array containing only the elements of the original array that pass the specified callback function.

Here is an example of how to use the array_filter function to check if an array is empty:

Output

Use array_reduce() function

To check if an array is empty using the array_reduce() function, you can pass the array and a callback function as arguments to the array_reduce() function, and then check if the returned value is equal to the initial value specified in the array_reduce() function.

Here is an example of how to use the array_reduce() function to check if an array is empty:

Output

That’s all about how to check if array is empty in PHP.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *