我有一个 MySQL 查询,它将转录表中的数据与列出其认证的表中的数据结合在一起。一些转录员可能拥有不止一项认证。
我发现有些抄写员会针对他们拥有的每个证书出现一次,这是他们应该做的。对于其他转录器,第一个数组值会被第二个覆盖。在我看来,如果转录器有两个证书,一个带有 certId 1,一个带有 certId 3,则 3 会覆盖 1。certId 为 2、4、5、6 或 7(任何组合)时不会发生这种情况除了 1 和 3)。
这是我的代码:
public function getTranscribersAndTheirCertificationsList() {
$Q = $this->read_db->query('SELECT t.*, tc.certId, tc.regNum, c.certName FROM transcriber t
JOIN transcribercertifications tc ON t.id = tc.tid
JOIN certifications c ON tc.certId = c.id');
return $Q->result_array();
}
下面的代码获取从上面传入的数据并对其进行处理:
$list = $this->MTranscriber->getTranscribersAndTheirCertificationsList();
foreach ($list as $t) {
$key = explode(' ', trim($t['name']));
$key = preg_replace('/[()]/','', $key);
$key = array_reverse($key);
$key = implode(' ', $key) . $t['id'];
$t['cet'] = $t['certName'] . ($t['regNum'] ? '-' . $t['regNum'] : '');
if ($t['certId'] == 2 || $t['certId'] == 5) {
$data['cetlist']['tta'][$key] = $t;
} else {
$data['cetlist']['cet'][$key] = $t;
}
}
if (!empty($data['cetlist']['cet'])) {
ksort($data['cetlist']['cet']);
}
if (!empty($data['cetlist']['tta'])) {
ksort($data['cetlist']['tta']);
}
如果我回显 $t,数据看起来不错。出于某种原因,当 $t 被添加到 else 分支中的每个数组时,第一个值在 certId = 1 和 certId = 3 的情况下被覆盖。我尝试使用 array_push 和 array_merge,但没有得到结果我也需要。
编辑这是一些经过清理的数据:
Array
(
[Doe John9] => Array
(
[id] => 9
[name] => John Doe
[email] =>
[homephone] =>
[cellphone] =>
[address1] => 1 Main Street
[address2] =>
[city] => ABC
[stateregion] =>
[zipcode] => 99999
[cet] => CET-485
[certId] => 3
[regNum] => 485
[certName] => CET
)
)
如果 John Doe 有另一个 certId 为 1 (CER) 的证书,它会被上面的数据覆盖,而我只能得到上面的那个。我应该拿一个 CER 和一个 CET。
最佳答案
用于将转录器保存到数组的 key 与 certId 相同。所以你总是会覆盖以前的条目。 如果您想添加多个版本的转录器,您应该将 certId 添加到 key 中。
关于php - 为什么有些数组值被覆盖而其他的没有? PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58932843/