1 回答

TA貢獻1796條經(jīng)驗 獲得超7個贊
可重用性、可維護性...如果你想測試許多可能的數(shù)組鍵,然后將它們添加到最終數(shù)組中,沒有什么能阻止你創(chuàng)建一個第三個數(shù)組,它將保存鍵以檢查并循環(huán)通過它:
<?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 關(guān)注
- 112 瀏覽
添加回答
舉報