Checking for the presence of all array elements in empty PHP

I am adding an array of elements from the form, and if they are all empty, I want to do some checking and add errors to the line. So I:

$array = array( 'RequestID' => $_POST["RequestID"], 'ClientName' => $_POST["ClientName"], 'Username' => $_POST["Username"], 'RequestAssignee' => $_POST["RequestAssignee"], 'Status' => $_POST["Status"], 'Priority' => $_POST["Priority"] ); 

And then, if all elements of the array are empty, do:

 $error_str .= '<li>Please enter a value into at least one of the fields regarding the request you are searching for.</li>'; 
+71
arrays php validation
Feb 18 '11 at 11:37
source share
12 answers

You can just use the built-in array_filter

If no callback is provided, all input entries equal to FALSE (see conversion to logical) will be deleted.

This can be done in one simple line.

 if(!array_filter($array)) { echo '<li>Please enter a value into at least one of the fields regarding the request you are searching for.</li>'; } 
+151
Feb 18 '11 at 11:51
source share

remove the array with empty glue and check the size of the resulting string:

 <?php if (strlen(implode($array)) == 0) echo 'all values of $array are empty'; ?> 
+17
Feb 18 '11 at 11:45
source share

An older question, but I thought I got into my solution, since it was not listed above.

 function isArrayEmpty($array) { foreach($array as $key => $val) { if ($val !== '') return false; } return true; } 
+7
Jul 19 '16 at 0:18
source share

you really don't need it.
You will check these fields separately, and by completing this process, you can determine if the array was empty (or contains invalid values, which is the same)

+3
Feb 18 '11 at 11:48
source share

You can delegate checking a fairly simple function:

 // returns true if $arr has a // value for _any_ of the $keys function check_if_any( $arr, $keys ) { foreach( $keys as $key ) { if( $arr[$key] ) return true; } return false; } $has_any = check_if_any( $_POST, array( "Username", "RequestAssignee", ... ) ); 
+1
Feb 18 '11 at 11:41
source share

It works for me, and

 if(!array_sum($array)){ //Values are empty do something } 
+1
May 6 '15 at 16:32
source share

Your definition of $ array is incorrect and has single quotes. It should read:

 $array = array( 'RequestID' => $_POST["RequestID"], 'ClientName' => $_POST["ClientName"], 'Username' => $_POST["Username"], 'RequestAssignee' => $_POST["RequestAssignee"], 'Status' => $_POST["Status"], 'Priority' => $_POST["Priority"] ); 
0
Feb 18 '11 at 11:43
source share

Late late answer, but probably the fastest and best way to do this:

 $array = array("demoKey1" => "", "demoKey2" => "2"); if(count(array_flip($array)) > 1){ // keys not empty // for example count is 2 } 
0
Nov 18 '16 at 13:27
source share

Let's simplify using this method:

 $is_empty = true; foreach ($array as $key => $value) { if ($value != '') $is_empty = false; } if ($is_empty) echo 'array is empty!'; else echo 'array is not empty!'; 
0
Jul 06 '19 at 12:50
source share

I had the same question, but I wanted to check each element in the array separately to see which one was empty. This turned out to be more complicated than expected, since you need to create key values ​​and actual values ​​in separate arrays in order to check and respond to an empty array element.

 print_r($requestDecoded); $arrayValues = array_values($requestDecoded); //Create array of values $arrayKeys = array_keys($requestDecoded); //Create array of keys to count $count = count($arrayKeys); for($i = 0; $i < $count; $i++){ if ( empty ($arrayValues[$i] ) ) { //Check which value is empty echo $arrayKeys[$i]. " can't be empty.\r\n"; } } 

Result:

 Array ( [PONumber] => F12345 [CompanyName] => Test [CompanyNum] => 222222 [ProductName] => Test [Quantity] => [Manufacturer] => Test ) 

Quantity cannot be empty.

-one
May 02 '18 at 6:27
source share

NOT FIXED BUT YOU get the logic :)

 $error = 0; foreach ($array as $k => $v){ if (empty($v)) { $error++; } } if ($error == count($array)) { $error_str .= '<li>Please enter a value into at least one of the fields regarding the request you are searching for.</li>'; } 
-2
Feb 18 '11 at 11:40
source share

it is pretty simple:

 foreach($array as $k => $v) { if(empty($v)) { unset($array[$k]); } } $show_error = count($array) == 0; 

you will also have to change the encapsulation of the array values ​​to double quotes.

-four
Feb 18 '11 at 11:56
source share



All Articles