Woocommerce linking variations

I found a solution with a recursive function

function decomposePrice($base, $iValue, $values, $prices) {

    $d = array();

    foreach ($prices as $i => $p) {

        $baseP = "$base{$values[$iValue][$i]}|";

        if (!is_array($p)) {
            $d[$baseP] = $p;
        } else {
            $d = array_merge($d, decomposePrice($baseP, $iValue + 1, $values, $p));
        }

    }

    return $d;
}


$decomposePrice = decomposePrice("", 0, $v_values, $v_price);


foreach ($decomposePrice as $att => $price) {


    $var_post = array(
                    'post_title'   => $ins["title"],
                    'post_content' => $ins["desc"] ,
                    'post_status'  => ($user->roles[0] === "pending_vendor") ? 'pending' : 'publish',
                    'post_parent' => $post_id,
                    'post_type' => 'product_variation',
                );

    // Insert the post into the database
    $var_post_id = wp_insert_post( $var_post );

    update_post_meta($var_post_id, '_price', $price);
    update_post_meta($var_post_id, '_regular_price', (string) $price);


    // attributes

    $list = explode("|", $att);
    array_pop($list);

    foreach ($list as $iName => $value) {
        update_post_meta($var_post_id, 'attribute_' . $v_name[$iName], $value);
    }

}