1 回答
TA贡献1796条经验 获得超7个赞
可重用性、可维护性...如果你想测试许多可能的数组键,然后将它们添加到最终数组中,没有什么能阻止你创建一个第三个数组,它将保存键以检查并循环通过它:
<?php
$another_array = [];
$array = [
'name' => 'Ivan The Terrible',
'mobile' => '1234567890',
'email' => 'tester@test.com'
];
$keysToCheck = [
// key_in_the_source_array => key_in_the_target
'name' => 'full_name',
'occupation' => 'occupation'
// if you want to test more keys, just add them there
];
foreach ($keysToCheck as $source => $target)
{
if (isset($array[$source]))
{
$another_array[0][$target] = $array[$source];
}
}
print_r($another_array);
请注意:
$another_array[0]['occupation'] = $array['occupation'] ??
print_r($another_array);
评估为
$another_array[0]['occupation'] = $array['occupation'] ?? print_r($another_array);
如果你在后面添加另一个,你会注意到,由于print_r()的返回值print_r($another_array);$another_array[0]['occupation'] => true
- 1 回答
- 0 关注
- 95 浏览
添加回答
举报