我有这个数组:
$ required_fields:
Array
(
[0] => email
)
$ posted_fields:
Array
(
[name] => Roberto
[email] =>
[richiesta] => richiesta
)
我需要检查$ required_fields的值是否在$ posted_fields中(如果它是null / empty)。
伪代码:
foreach ($required_fields as $value_r)
{
foreach ($posted_fields as $key_p=>$value_p)
{
if (in_array($value_r,$posted_fields)
{
if ($value_p=='' || $value_p == NULL)
{
// others stuff
}
}
}
}
答案 0 :(得分:1)
为什么选择简单的东西,比如
foreach ($required_fields as $required_value)
{
if(!isset($posted_fields[$required_value]) || trim($posted_fields[$required_value]) == "")
{
//Mandatory field not set or empty
}
}
答案 1 :(得分:0)
在您提供的代码中,我会更改if (in_array($value_r,$posted_fields)
行
foreach ($required_fields as $value_r)
{
foreach ($posted_fields as $key_p => $value_p)
{
if ($value_r == $key_p)
{
if ($value_p=='' || $value_p == NULL)
{
// others stuff
}
}
}
}
答案 2 :(得分:0)
foreach ($required_fields as $value_r) {
if(array_key_exists($value_r, $posted_fields)) {
// value exists in key
}
}