0

例如,我有 5 个变量(字段值),

$companyname
$firstname,
$lastname,
$address1,
$address2

所以,根据要求,这里有2组。

$group1 = array('firstname','lastname','address1');
$group2 = array('companyname','address2');

Group1 变量是必需的,Group2 变量是必需的。IE

Either $firstname AND $lastname AND $address1 Can Not Be Blank 
OR
$companyname AND $address2 Can Not Be Blank.

目标是:

这个函数:$alt_req_arrays($arr1,$arr2); 应该返回真或假。

问题是: 我想用最少的代码编写这个函数(使用数组函数或类似的东西......)

4

1 回答 1

0

如果你形成你的 $group 就像:

$group1 = array('firstname' => 'dfgdf','lastname' => 'dsfsad','address1' => 'ccc');
$group2 = array('companyname' => 'a','address2' => 'fdgdf');

即hash "field" => "value of the field",那么可以使用如下:

$group1 = array('firstname' => 'dfgdf','lastname' => 'dsfsad','address1' => 'ccc');
$group2 = array('companyname' => 'a','address2' => 'fdgdf');

function alt_req_arrays($arr1,$arr2)
{
    return !array_search(null,$arr1) || !array_search(null,$arr2);
}
var_dump(alt_req_arrays($group1,$group2));
于 2012-05-11T14:44:51.237 回答