简体   繁体   English

检查所有数组项是否为空PHP

[英]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 您可以使用内置的array_filter

If no callback is supplied, all entries of input equal to FALSE (see converting to boolean) will be removed. 如果没有提供回调,则将删除所有输入等于FALSE的条目(请参阅转换为布尔值)。

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. 您对$ array的定义不正确,并且有单引号。 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. 您还必须将数组值的封装更改为双引号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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