由于您在阵列中有插件选项(带键),让我们假设您的场景如下:
你的old 选项为$old = get_option( \'plugin_options\' );
包含以下内容:
$old = array(
\'plugin_option_1\' => \'1\',
\'plugin_option_2\' => \'2\',
\'plugin_option_3\' => \'3\',
\'plugin_option_4\' => \'4\',
\'plugin_option_5\' => \'5\'
);
您的
new 选项为
$new
包含此数组以更新:
$new = array(
\'plugin_option_2\' => \'9\',
\'plugin_option_5\' => \'10\'
);
Now on plugin update foreach ( $old as $key => $value )
$old[ $key ] = ( ! isset( $new[ $key ] ) ) ? \'\' : $new[ $key ];
update_option( \'plugin_options\', $old );
With PHP 7
foreach ( $old as $key => $value )
$old[ $key ] = ( $new[ $key ] ) ?? \'\';
update_option( \'plugin_options\', $old );
Result on print_r( $old )
:
Array
(
[plugin_option_1] =>
[plugin_option_2] => 9
[plugin_option_3] =>
[plugin_option_4] =>
[plugin_option_5] => 10
)