간단히 말해 두 개의 간단한 배열이 있는데 두 번째 배열의 특정 키에 빈 값이 있는지 확인한 다음 첫 번째 배열의 해당 값으로 바꿉니다.PHP 다차원 배열의 특정 요소를 비교하고 변경하십시오.
예 :
$a1 = [ 1 => 'one', 2 => 'two', 3 => 'three',5=>'cinco', 6=>'six'];
$a2 = [ 2 => 'two', 5=>'five', 6=>'' ];
결과 :
이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);
하지만 두 개의 2D 배열에 이것을 적용하려면 어떻게해야합니까? 이 경우 적절한 접근 방법은 무엇입니까? 의이 두 차원 배열이 될 것입니다 가정 해 봅시다 :
$superheroes_complete = array(
"spiderman" => array(
"name" => "Peter Parker",
"email" => "[email protected]",
),
"superman" => array(
"name" => "Clark Kent",
"email" => "[email protected]",
),
"ironman" => array(
"name" => "Harry Potter",
"email" => "[email protected]",
)
);
$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]",
),
"super-man" => array(
"name" => "Clark Kent",
"email" => "something",
),
"iron-man" => array(
"name" => "Harry Potter",
"email" => "another one",
)
);
많은 감사 사전에 감사합니다! 당신은 단지 "이메일 알아서해야하는 경우
foreach ($superheroes_complete as $hero => $info) {
foreach ($info as $key => $value) {
if (empty($superheroes_empty[$hero][$key])) {
$superheroes_empty[$hero][$key] = $value;
}
}
}
루프 용으로 중첩 – ebyt