Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i check if all keys in an array have empty values in PHP?

Tags:

arrays

php

I have an array

$array = array('key1' => null, 'key2' => null, 'key3' => null, 'key4' => null);

i would like to determine if all the array keys have empty values if so then return false. the above example should return false as it does not have any value. but if one or more keys have any values then it should return true for example the below example is true.

$array = array('key1', 'key2' => value2, 'key3', 'key4' => value4);
like image 220
Ibrahim Azhar Armar Avatar asked Jun 14 '11 06:06

Ibrahim Azhar Armar


People also ask

How do you check if all elements in an array are empty?

To check if all of the values in an array are equal to null , use the every() method to iterate over the array and compare each value to null , e.g. arr. every(value => value === null) . The every method will return true if all values in the array are equal to null .

What is array_keys () used for in PHP?

The array_keys() function returns an array containing the keys.

How do you check if an array has a value in PHP?

The in_array() function is an inbuilt function in PHP that is used to check whether a given value exists in an array or not. It returns TRUE if the given value is found in the given array, and FALSE otherwise.


1 Answers

Assuming you actually mean an array like

array('key1' => null, 'key2' => null, 'key3' => null, 'key4' => null)

the answer is simply

if (!array_filter($array)) {
    // all values are empty (where "empty" means == false)
}
like image 159
deceze Avatar answered Sep 21 '22 09:09

deceze