Issue with foreach on duplicate meta_key’s

Here is an example of getting the user IDs, looping through each, grabbing a meta_key as an array, adding new values to it and remove old values, saving the meta back, then grabbing it again to loop through each item in the array for output. Array is just an example of duplicate keys.

$user_query = new WP_User_Query( array( 'fields' => 'ID' ));
$user_ids = $user_query->get_results();

echo "<ul>";

foreach ( $user_ids as $user_id ) {

    // all meta for user
    $all_meta_for_user = array_map( function( $a ){ return $a[0]; }, get_user_meta( $user_id ) );

    echo "<pre>";
    print_r ( $all_meta_for_user ); // see all the values
    echo "</pre>";

    $key = '__TEST_KEY';
    $value_set = array();
    if( isset( $all_meta_for_user [ $key ] )) {
        $value_set = get_user_meta ( $user_id, $key, true );
        if( ! is_array( $value_set )) $value_set = array(); // make sure we're working with an array
    }

    // add to existing values
    $value_set[] = timer_stop(4);

    // remove old items
    if( count ($value_set) > 4 ) array_shift ($value_set);

    // set the meta
    update_user_meta($user_id, $key, $value_set );

    // LATEST VALUES
    $value_set = get_user_meta ( $user_id, $key, true );

    echo "<pre>";
    print_r ( $value_set ); // the value set currently in our meta
    echo "</pre>";

    foreach ( $value_set as $inx => $value ){
        echo "<li>{$user_id} - {$key} - {$inx} - {$value}</li>";
    }
    echo "</ul>";
}

echo "</ul>";