php check array empty

Check whether an array is empty [duplicate]

I have the following code

12 Answers 12

There are two elements in array and this definitely doesn’t mean that array is empty. As a quick workaround you can do following:

Otherwise in your particular case empty() construct will always return true if there is at least one element even with «empty» value.

php check array empty. Смотреть фото php check array empty. Смотреть картинку php check array empty. Картинка про php check array empty. Фото php check array empty

You can also check it by doing.

Try to check it’s size with sizeof if 0 no elements.

PHP’s built-in empty() function checks to see whether the variable is empty, null, false, or a representation of zero. It doesn’t return true just because the value associated with an array entry is false, in this case the array has actual elements in it and that’s all that’s evaluated.

If you’d like to check whether a particular error condition is set to true in an associative array, you can use the array_keys() function to filter the keys that have their value set to true.

You can then use the empty() function to check whether this array is empty, simultaneously telling you whether there are errors and also which errors have occurred.

array with zero elements converts to false

However, empty($error) still returns true, even though nothing is set.

That’s not how empty() works. According to the manual, it will return true on an empty array only. Anything else wouldn’t make sense.

Источник

How do I check if array value is empty?

Here is my array ouput

How do I know the [3] => is empty?

My out put showing all is not empty. What is correct way to check is empty?

php check array empty. Смотреть фото php check array empty. Смотреть картинку php check array empty. Картинка про php check array empty. Фото php check array empty

8 Answers 8

php check array empty. Смотреть фото php check array empty. Смотреть картинку php check array empty. Картинка про php check array empty. Фото php check array empty

It works as expected, third one is empty

Maybe try to trim its value, just in case that third value would be just a space.

You can check for an empty array by using the following:

php check array empty. Смотреть фото php check array empty. Смотреть картинку php check array empty. Картинка про php check array empty. Фото php check array empty

You can use array_diff() and array_diff_key() :

array_diff() extracts all items which are not the same (therefore leaving out the blanks), array_diff_key gives back the differences to the original array.

Here is a simple solution to check an array for empty key values and return the key.

To check if array contains an empty key value. Try this.

php check array empty. Смотреть фото php check array empty. Смотреть картинку php check array empty. Картинка про php check array empty. Фото php check array empty

An other solution:

php check array empty. Смотреть фото php check array empty. Смотреть картинку php check array empty. Картинка про php check array empty. Фото php check array empty

im using in my project like this for check this array

im posting form data like this array(‘username’ => ‘john’,’surname’ => ‘sins’);

php check array empty. Смотреть фото php check array empty. Смотреть картинку php check array empty. Картинка про php check array empty. Фото php check array empty

Not the answer you’re looking for? Browse other questions tagged php or ask your own question.

Linked

Related

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

site design / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2021.9.17.40238

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Источник

How to check whether an array is empty using PHP?

21 Answers 21

If you just need to check if there are ANY elements in the array

If you need to clean out empty values before checking (generally done to prevent explode ing weird strings):

An empty array is falsey in PHP, so you don’t even need to use empty() as others have suggested.

Some decent answers, but just thought I’d expand a bit to explain more clearly when PHP determines if an array is empty.

An array with a key (or keys) will be determined as NOT empty by PHP.

As array values need keys to exist, having values or not in an array doesn’t determine if it’s empty, only if there are no keys (AND therefore no values).

So checking an array with empty() doesn’t simply tell you if you have values or not, it tells you if the array is empty, and keys are part of an array.

So consider how you are producing your array before deciding which checking method to use.
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.

Take these arrays for example:

If you echo out the array keys and values for the above arrays, you get the following:

ARRAY ONE:
[UserKeyA] => [UserValueA]
[UserKeyB] => [UserValueB]

ARRAY TWO:
[0] => [UserValue01]
[1] => [UserValue02]

And testing the above arrays with empty() returns the following results:

ARRAY TWO:
$ArrayTwo is not empty

ARRAY THREE:
$ArrayThree is not empty

An array will always be empty when you assign an array but don’t use it thereafter, such as:

This will be empty, ie PHP will return TRUE when using if empty() on the above.

In this case, you can loop the array in a foreach, testing if each key has a value. This is a good method if you need to run through the array anyway, perhaps checking the keys or sanitising data.

However it is not the best method if you simply need to know «if values exist» returns TRUE or FALSE. There are various methods to determine if an array has any values when it’s know it will have keys. A function or class might be the best approach, but as always it depends on your environment and exact requirements, as well as other things such as what you currently do with the array (if anything).

Here’s an approach which uses very little code to check if an array has values:

Using array_filter() :
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.

Running array_filter() on all three example arrays (created in the first code block in this answer) results in the following:

ARRAY TWO:
$arraytwo is not empty

ARRAY THREE:
$arraythree is empty

So when there are no values, whether there are keys or not, using array_filter() to create a new array and then check if the new array is empty shows if there were any values in the original array.
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.

I’m not experienced in checking overheads, but it would be good to know the differences between using array_filter() and foreach checking if a value is found.

Obviously benchmark would need to be on various parameters, on small and large arrays and when there are values and not etc.

php check array empty. Смотреть фото php check array empty. Смотреть картинку php check array empty. Картинка про php check array empty. Фото php check array empty

If you’d like to exclude the false or empty rows (such as 0 => » ), where using empty() will fail, you can try:

array_filter() : If no callback is supplied, all entries of array equal to FALSE (see converting to boolean) will be removed.

If you’d like to remove all NULL, FALSE and empty strings ( » ), but leave zero values ( 0 ), you can use strlen as a callback, e.g.:

If you want to ascertain whether the variable you are testing is actually explicitly an empty array, you could use something like this:

php check array empty. Смотреть фото php check array empty. Смотреть картинку php check array empty. Картинка про php check array empty. Фото php check array empty

I ran the benchmark included at the end of the post. To compare the methods:

and got the following results

The difference between empty and casting to a boolean are insignificant. I’ve run this test multiple times and they appear to be essentially equivalent. The contents of the arrays do not seem to play a significant role. The two produce the opposite results but the logical negation is barely enough to push casting to winning most of the time so I personally prefer empty for the sake of legibility in either case.

php check array empty. Смотреть фото php check array empty. Смотреть картинку php check array empty. Картинка про php check array empty. Фото php check array empty

Why has no one said this answer:

php check array empty. Смотреть фото php check array empty. Смотреть картинку php check array empty. Картинка про php check array empty. Фото php check array empty

if you are to check the array content you may use:

php check array empty. Смотреть фото php check array empty. Смотреть картинку php check array empty. Картинка про php check array empty. Фото php check array empty

In my opinion the simplest way for an indexed array would be simply:

An ‘if’ condition on the array would evaluate to true if the array is not empty and false if the array is empty. This is not applicable to associative arrays.

php check array empty. Смотреть фото php check array empty. Смотреть картинку php check array empty. Картинка про php check array empty. Фото php check array empty

But note that if the array has a large number of keys, this code will spend much time counting them, as compared to the other answers here.

php check array empty. Смотреть фото php check array empty. Смотреть картинку php check array empty. Картинка про php check array empty. Фото php check array empty

You can use array_filter() which works great for all situations:

Making the most appropriate decision requires knowing the quality of your data and what processes are to follow.

Might your string value contain a 0 that you want to deem true/valid/non-empty? If so, then you only need to check if the column value has length.

Why have I gone to such length to explain this very basic task?

Источник

best way to check a empty array?

How can I check an array recursively for empty content like this example:

The array is not empty but there is no content. How can I check this with a simple function?

11 Answers 11

If your array is only one level deep you can also do:

Works in most cases 🙂

php check array empty. Смотреть фото php check array empty. Смотреть картинку php check array empty. Картинка про php check array empty. Фото php check array empty

Solution with array_walk_recursive:

Assuming the array will always contain the same type of data:

Short circuiting included.

Here’s my version. Once it finds a non-empty string in an array, it stops. Plus it properly checks on empty strings, so that a 0 (zero) is not considered an empty string (which would be if you used empty() function). By the way even using this function just for strings has proven invaluable over the years.

If anyone stumbles on this question and needs to check if the entire array is NULL, meaning that each pair in the array is equal to null, this is a handy function. You could very easily modify it to return true if any variable returns NULL as well. I needed this for a certain web form where it updated users data and it was possible for it to come through completely blank, therefor not needing to do any SQL.

Returns TRUE if passed a variable other than an array, or if any of the nested arrays contains a value (including falsy values!). Returns FALSE otherwise. Short circuits.

Here’s a good utility function that will return true (1) if the array is empty, or false (0) if not:

For example, given a multidimensional array:

View this code interactively at: http://codepad.org/l2C0Efab

I needed a function to filter an array recursively for non empty values.

Here is my recursive function:

array_slice($result, 0) has the effect that numeric indices will be rearranged (0..length-1).

Источник

Check if all items of array are empty

I’m beginner in the PHP programming. Please can somebody suggest me any trick? How could I shorten this script with any cycle function?

I think that it is possible to make it somehow. But I don’t know how.

php check array empty. Смотреть фото php check array empty. Смотреть картинку php check array empty. Картинка про php check array empty. Фото php check array empty

6 Answers 6

you can use array_filter built in function,

This simple answer is tailored to user’s example question (it does not take into account arrays whose values are arrays/objects etc).

You should do that check before you access any Key in the POST array.

Required no loop at all, that foreach is just used to output missing keys, not to do the check

Basically you create a function which will determine the length of an array element and return it’s length.

You then map this function to all all elements and then sum it.

php check array empty. Смотреть фото php check array empty. Смотреть картинку php check array empty. Картинка про php check array empty. Фото php check array empty

php check array empty. Смотреть фото php check array empty. Смотреть картинку php check array empty. Картинка про php check array empty. Фото php check array empty

Linked

Related

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

site design / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2021.9.17.40238

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Источник

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *