希望使用多个 foreach 循环压缩准备好的语句 (PHP, SQLite)

Looking to Condense a prepared statement with multiple foreach loops (PHP, SQLite)

我有一个 JSON FILE 被解析并通过 INSERT OR IGNORE 加上一个 UPDATE 查询输入到我的数据库中。 JSON 被拆分为 "goalies"、"forwards" 和 "defensemen" 所以我最终这样做了:

try {   
        $db = new PDO('connection stuff');  
        $db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);  
    } catch (Exception $e) {  
         echo "Error: Could not connect to database.  Please try again later.";
         exit;
    }   
    $json = file_get_contents('http://nhlwc.cdnak.neulion.com/fs1/nhl/league/teamroster/SJS/iphone/clubroster.json');
    $json = json_decode($json, TRUE);

    $goalie = $json['goalie'];
    $defensemen = $json['defensemen'];
    $forwards = $json['forwards'];

    $query = "INSERT OR IGNORE INTO roster2015_2016 ('position','id','weight','height','imageURL','birthplace','age','name','birthdate','number')
              VALUES (:position, :id, :weight, :height , :imageURL, :birthplace , :age , :name , :birthdate , :number)";
    $prepare = $db->prepare($query);

    $query2 = "UPDATE roster2015_2016
              SET position= :position, id= :id, weight= :weight, height= :height, imageURL= :imageURL, birthplace= :birthplace, age= :age, name= :name,
                             birthdate= :birthdate, number= :number
              WHERE id= :id"; 
    $prepare2 = $db->prepare($query2);

    foreach ($goalie as $pd) {
        $a = array (':position' => $position = $pd['position'],
                                ':id' => $id = $pd['id'],
                                ':weight' => $weight = $pd['weight'],
                                ':height' => $height = $pd['height'],
                                ':imageURL' => $imageURL = $pd['imageUrl'],
                                ':birthplace' =>$birthplace = $pd['birthplace'],
                                ':age' => $age = $pd['age'],
                                ':name' => $name = $pd['name'],
                                ':birthdate' => $birthdate = $pd['birthdate'],
                                ':number' => $number = $pd['number']);

        $prepare->execute($a);
        $prepare2->execute($a);
    }

        foreach ($defensemen as $pd) {
        $a = array (':position' => $position = $pd['position'],
                                ':id' => $id = $pd['id'],
                                ':weight' => $weight = $pd['weight'],
                                ':height' => $height = $pd['height'],
                                ':imageURL' => $imageURL = $pd['imageUrl'],
                                ':birthplace' =>$birthplace = $pd['birthplace'],
                                ':age' => $age = $pd['age'],
                                ':name' => $name = $pd['name'],
                                ':birthdate' => $birthdate = $pd['birthdate'],
                                ':number' => $number = $pd['number']);

        $prepare->execute($a);
        $prepare2->execute($a);
    }

        foreach ($forwards as $pd) {
        $a = array (':position' => $position = $pd['position'],
                                ':id' => $id = $pd['id'],
                                ':weight' => $weight = $pd['weight'],
                                ':height' => $height = $pd['height'],
                                ':imageURL' => $imageURL = $pd['imageUrl'],
                                ':birthplace' =>$birthplace = $pd['birthplace'],
                                ':age' => $age = $pd['age'],
                                ':name' => $name = $pd['name'],
                                ':birthdate' => $birthdate = $pd['birthdate'],
                                ':number' => $number = $pd['number']);

        $prepare->execute($a);
        $prepare2->execute($a);
    }

如您所见,我连续 3 次做同样的事情(进攻、防守和守门员各 1 次)。我尝试使用 && 运算符并只创建一个 foreach 循环,但我猜你不能这样做。那个失败的尝试是有人 link 可以给我一个资源让我快速知道该怎么做吗?我尝试了一些 Google 和 Stack Overflow 搜索,但找不到我要找的东西。 Here is a link to my roster output。不要担心滚动到查询的右侧,它工作得很好。

试试这个

$json = file_get_contents('http://nhlwc.cdnak.neulion.com/fs1/nhl/league/teamroster/SJS/iphone/clubroster.json');
$json = json_decode($json, TRUE);
$resultArray = array();


$goalie = array();
if (is_array($json['goalie']) && count($json['goalie']) > 0) {
    $goalie = $json['goalie'];
}

$defensemen = array();
if (is_array($json['defensemen']) && count($json['defensemen']) > 0) {
    $defensemen = $json['defensemen'];
}
$resultArray = array_merge($goalie, $defensemen);

$forwards = array();
if (is_array($json['forwards']) && count($json['forwards']) > 0) {
    $forwards = $json['forwards'];
}

$resultArray = array_merge($resultArray, $forwards);

foreach ($resultArray as $pd) {
    $a = array(':position' => $position = $pd['position'],
        ':id' => $id = $pd['id'],
        ':weight' => $weight = $pd['weight'],
        ':height' => $height = $pd['height'],
        ':imageURL' => $imageURL = $pd['imageUrl'],
        ':birthplace' => $birthplace = $pd['birthplace'],
        ':age' => $age = $pd['age'],
        ':name' => $name = $pd['name'],
        ':birthdate' => $birthdate = $pd['birthdate'],
        ':number' => $number = $pd['number']);

    $prepare->execute($a);
    $prepare2->execute($a);
}