嵌套的foreach循环 - 更高效的东西?

时间:2013-08-27 17:46:55

标签: php foreach

所以我有嵌套的foreach循环,我想知道是否有更有效的方法来执行此操作,代码方式:

foreach ($arraykey as $columnname => $value) {
    foreach ($records as $field_entity_id) {    
        $fieldinsertloop = "('registration', 'reg_type', 0,".$field_entity_id.",".$field_entity_id.", 'und', 0,'".$value."','".$value."')";
    }
    $sql = "INSERT INTO " .str_replace("multi_reg", "field_data", $columnname);
    $sql .= " (entity_type, bundle, deleted, revision_id, entity_id, language, delta, ".str_replace("multi_reg_", "", $columnname)."_value, ".str_replace("multi_reg_", "", $columnname)."_format)";
    $sql .= " VALUES ";
    $sql .= $fieldinsertloop;
    db_query($sql);
}

基本上,我想要优化速度,然后减少编写的代码 - 按顺序或最佳两者。

1 个答案:

答案 0 :(得分:1)

您可以通过引用使用值来从循环中获得更多性能:

foreach ($arraykey as $columnname => &$value) {

因此&$value,但请注意,每次在$value之后执行某些操作时,它都会影响原始的$arraykey[$columnname]值。此外,不确定你是否正确嵌套foreach。我会移动嵌套foreach的结束括号:

// $value passed by reference
foreach ($arraykey as $columnname => &$value) {

    // Pass $field_entity_id by reference
    foreach ($records as &$field_entity_id) {

        $fieldinsertloop = "('registration', 'reg_type', 0,".$field_entity_id.",".$field_entity_id.", 'und', 0,'".$value."','".$value."')";

        $sql = "INSERT INTO " .str_replace("multi_reg", "field_data", $columnname);
        $sql .= " (entity_type, bundle, deleted, revision_id, entity_id, language, delta, ".str_replace("multi_reg_", "", $columnname)."_value, ".str_replace("multi_reg_", "", $columnname)."_format)";
        $sql .= " VALUES ";
        $sql .= $fieldinsertloop;

        db_query($sql);

    } // End of nested foreach moved here
}



您的代码的另一个问题是,您似乎正在使用Drupal db_query(),它有更好的方法来插入值。如果是,您可以将代码更改为:

foreach ($arraykey as $columnname => &$value) {

    $table = str_replace("multi_reg", "field_data", $columnname);
    $columns = array('entity_type', 'bundle', 'deleted', 'revision_id', 'entity_id', 'language', 'delta', str_replace("multi_reg_", "", $columnname).'_value', str_replace("multi_reg_", "", $columnname).'_format');

    foreach ($records as &$field_entity_id) {

        $values = array('registration', 'reg_type', 0, $field_entity_id, $field_entity_id, 'und', 0, $value, $value);

        //Build Associative Array for Insert
        for( $x = 0, $max = count($values); $x < $max; $x++ ) {
            $sqlValues[$columns[$x]] = $values[$x];
        }

        db_insert($table)->fields($columns)->values($sqlValues)->execute();
    }
}

foreach ($arraykey as $columnname => &$value) {

    $table = str_replace("multi_reg", "field_data", $columnname);
    $columns = array('entity_type', 'bundle', 'deleted', 'revision_id', 'entity_id', 'language', 'delta', str_replace("multi_reg_", "", $columnname).'_value', str_replace("multi_reg_", "", $columnname).'_format');
    $query = db_insert($table)->fields($columns);

    foreach ($records as &$field_entity_id) {

        $values = array('registration', 'reg_type', 0, $field_entity_id, $field_entity_id, 'und', 0, $value, $value);

        //Build Associative Array for Insert
        for( $x = 0, $max = count($values); $x < $max; $x++ ) {
            $sqlValues[$columns[$x]] = $values[$x];
        }

        $query->values($sqlValues);
    }
    $query->execute();
}

第二个例子应该更快,但有更多行。它等待,直到在执行之前添加了所有值。使用db_insert()要求值位于关联数组中,列名称为键。

干净怎么样?