简体   繁体   中英

Checking if all the array items are empty PHP

I'm adding an array of items from a form and if all of them are empty, I want to perform some validation and add to an error string. So I have:

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

And then if all of the array elements are empty perform:

$error_str .= '<li>Please enter a value into at least one of the fields regarding the request you are searching for.</li>';

You can just use the built in array_filter

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

So can do this 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>';
}

使用空胶水内爆数组并检查结果字符串的大小:

<?php if (strlen(implode($array)) == 0) echo 'all values of $array are empty'; ?>

An older question but thought I'd pop in my solution as it hasn't been listed above.

function isArrayEmpty($array) {
    foreach($array as $key => $val) {
        if ($val !== '')
            return false;
    }
    return true;
}

you don't really need it.
You're going to validate these fields separately and by finishing this process you can tell if array was empty (or contains invalid values, which is the same)

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"] );

simplify use this way:

$array = []; //target array
$is_empty = true; //flag

foreach ($array as $key => $value) {
    if ($value != '')
        $is_empty = false;
}
if ($is_empty)
    echo 'array is empty!';
else
    echo 'array is not empty!';

I had the same question but wanted to check each element in the array separately to see which one was empty. This was harder than expected as you need to create the key values and the actual values in separate arrays to check and respond to the 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 can't be empty.

NOT TESTED BUT u 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>';
}

this is pretty simple:

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

you would also have to change your encapsulation for your array values to double quotes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM