php array_merge without erasing values?

前端 未结 6 1066
南旧
南旧 2020-12-16 15:23

Background: Trevor is working with a PHP implementation of a standard algorithm: take a main set of default name-value pairs, and update those name-value pa

6条回答
  •  难免孤独
    2020-12-16 15:56

    Adjust to your needs:

    # Replace keys in $foo
    foreach ($foo as $key => $value) {
        if ($value != '' || !isset($bar[$key])) continue;
        $foo[$key] = $bar[$key];
    }
    
    # Add other keys in $bar
    # Will not overwrite existing keys in $foo
    $foo += $bar;
    

提交回复
热议问题