update the same custom field without duplicates code example

Example 1: update the same custom field without duplicates

add_post_meta( $post->ID, 'my_foo_bar', 'value', true );

Example 2: update the same custom field without duplicates

<?php
    define('WP_USE_THEMES', false);
    require('wp-blog-header.php');

    define( 'WP_DEBUG_DISPLAY', true ); 
    ini_set( 'display_errors', true );
    $allposts = get_posts('numberposts=-1&post_type=post&post_status=any');
    $keys = array('podPressPostSpecific', 'aktt_tweeted', 'podPressMedia');
    foreach ( $keys as $key ) {
        foreach( $allposts as $postinfo) {
            // Fetch array of custom field values
            $postmeta = get_post_meta($postinfo->ID, $key);

            if (!empty($postmeta) ) {
                // Delete the custom field for this post (all occurrences)
                delete_post_meta($postinfo->ID, $key);

                // Insert one and only one custom field
                update_post_meta($postinfo->ID, $key, $postmeta[0]);
            }
        }
    }
?>