如何循环遍历数组并写入mysql非重复数据?

i have an array called MyArray i want to insert its data into mysql but first need to check if it exist in mysql already. could any one tell me how i can loop through array and how to reference each variable inside the array then insert those data in to mysql?

$MyArray[] = array(  
        "usernameVar" => htmlspecialchars($usernameVar),
        "profPic" => htmlspecialchars($profPic), 
        "idVar" => htmlspecialchars($idVar), 
        "standardResolution" => htmlspecialchars($standardResolution),  
        "imagePageLink" => htmlspecialchars($imagePageLink),
        "createdTimeVal" => htmlspecialchars($createdTimeVal),
        "imageTags" => htmlspecialchars($imageTags),
);  

$MyArray = array_reverse( $MyArray );

//now i need to loop through array and insert non duplicate data in to mysql

$result = mysql_query("SELECT imageUrl FROM mytable WHERE imageUrl = '$standardResolution'");
if (!$result) {
    die('Invalid query: ' . mysql_error());
}
if(mysql_num_rows($result) == 0) {
    $j++;
    echo "<a href='".$imagePageLink."'><img src='".$standardResolution."'  width='150' height='150' border='0'></a><br> ";

    // row not found, do stuff...
    echo "New Users(".$i."):";
    echo "<br>User Name(".$i."):".$usernameVar;

    $result2 =  mysql_query("INSERT INTO mytable (ID, username, profile_picture, instaId, imageUrl,imagePageURL, CreatedTime, imageTags, date) VALUES('$ID','$usernameVar','$ProfilePicVar','$idVar','$standardResolution','$imagePageLink','$createdTimeVal','$imageTags',NOW())");

    if (!$result2) {
        die('Invalid query: ' . mysql_error());
    }
} 
else 
{
    $m++;

    // do other stuff...
    echo "Already Added Users(".$i."):";
    echo "<br>User Name(".$i."):".$usernameVar;
};

See INSERT INTO ... ON DUPLICATE KEY UPDATE ... syntax:

http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html

You can loop through an array using foreach in PHP:

foreach($MyArray as $key=>$value){
    ....
}

Inserting from the array is a little more complicated, but in principle, you loop over each variable in the array and create a string.

foreach($MyArray as $key=>$value){
    $names .= $key.",";
    $values .= "'$value',";
}
$names = substr($names, 0, strlen($names) - 1);
$values = substr($values, 0, strlen($values) - 1);

then do:

mysql_query("INSERT INTO table ($names) VALUES($values)");