简而言之,我有两个简单的数组,我想验证第二个数组中的某些键是否具有空值,并将它们替换为第一个数组中的对应值。
Example:
$a1 = [ 1 => 'one', 2 => 'two', 3 => 'three',5=>'cinco', 6=>'six'];
$a2 = [ 2 => 'two', 5=>'five', 6=>'' ];
Result:
Array
(
[2] => two
[5] => five
[6] => six
)
以下代码已经适用于此。
$keys = array_keys($a1);
foreach ($keys as $k)
{
if ((isset($a2[$k])) && (($a2[$k]) == '')) {
$a2[$k] = $a1[$k];
}
}
print_r($a2);
但是如果我们想将其应用于两个二维数组怎么办?在这种情况下,正确的方法是什么?假设这两个二维数组是:
$superheroes_complete = array(
"spiderman" => array(
"name" => "Peter Parker",
"email" => "[email protected] /cdn-cgi/l/email-protection",
),
"superman" => array(
"name" => "Clark Kent",
"email" => "[email protected] /cdn-cgi/l/email-protection",
),
"ironman" => array(
"name" => "Harry Potter",
"email" => "[email protected] /cdn-cgi/l/email-protection",
)
);
$superheroes_empty = array(
"spiderman" => array(
"name" => "Peter Parker",
"email" => "",
),
"superman" => array(
"name" => "Clark Kent",
"email" => "something",
),
"ironman" => array(
"name" => "Harry Potter",
"email" => "another one",
)
);
期待:
$superheroes = array(
"spider-man" => array(
"name" => "Peter Parker",
"email" => "[email protected] /cdn-cgi/l/email-protection",
),
"super-man" => array(
"name" => "Clark Kent",
"email" => "something",
),
"iron-man" => array(
"name" => "Harry Potter",
"email" => "another one",
)
);
非常感谢并提前感谢您!