问题描述
我有一个名为 $info
的关联数组。为简单起见,我在 $info
中放置了 4 个键,但我真正的问题有大约 50 个可能的键。我想检查是否设置了特定的键。但是,我想为它编写一个函数,而不是像在当前示例中那样重复 isset()
5 次。所以我写了一个名为 set_a_val()
的函数,如果它被设置则返回值,否则返回 NULL
。通过这种方式,我可以直接为 $gender
变量分配一个类似于 $gender = set_a_val($info['gender']);
的值,我不想在整个数组中解析,因为我每次都必须对大约 3000 $info
个数组重复此操作。>
问题是,如果我解析像 name
这样没有设置到 set_a_val()
函数中的键,我会得到一个错误。我该怎么做?
$info [
'gender' => 'M','age' => 43,'Job Title' => 'Doctor','Experience' => 10
];
$gender = NULL;
$age = NULL;
$job_title = NULL;
$experience = NULL;
$name = NULL;
if (isset($info['gender'])) {
$gender = $info['gender'];
}
if (isset($info['age'])) {
$age = $info['age'];
}
if (isset($info['Job Title'])) {
$job_title = $info['Job Title'];
}
if (isset($info['Experience'])) {
$experience = $info['Experience']
}
if (isset($info['Name'])) {
$name = $info['Name']
}
function set_a_val($val) {
if (isset($val)) {
return $val;
}
return NULL;
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)