在php中,有没有更好的方法可以按行循环二维数组

在php中,有没有更好的方法可以按行循环二维数组,php,arrays,loops,foreach,Php,Arrays,Loops,Foreach,我通过2D数组上的循环在表中插入多个数据 这是我的数据: $data['id'] = array([0] => '1', [1] => '2'); $data['name'] = array([0] => 'Ben', [1] => 'Dan'); $data['status'] = array([0] => 'Active', [1] => 'Active'); $data['updatedby'] = '1'; $data['updateddate'] =

我通过2D数组上的循环在表中插入多个数据

这是我的数据:

$data['id'] = array([0] => '1', [1] => '2');
$data['name'] = array([0] => 'Ben', [1] => 'Dan');
$data['status'] = array([0] => 'Active', [1] => 'Active');
$data['updatedby'] = '1';
$data['updateddate'] = date('d-M-y H:i');
$idColumn = 'id';
$table = 'tablename';
这是我的代码:

foreach($data as $key => $value){
    $i = 0;
    if($key <> 'updatedby' && $key <> 'updateddate'){
        foreach($value as $row){
            $result = $this->myModel->recordUpdate($idColumn, $data['id'][$i], array($key => $row), $table);
            $i = $i + 1;
        }
    }

    if($key == 'updatedby'){
        foreach($data['id'] as $row){
            $result = $this->myModel->recordUpdate($idColumn, $row, array($key => $data['updatedby']), $table);
        }
    }

    if($key == 'updateddate'){
        foreach($data['id'] as $row){
            $result = $this->myModel->recordUpdate($idColumn, $row, array($key => $data['updateddate']), $table);
        }
    }
}

function recordUpdate($idColumn, $id, $data, $table)
{
    $this->db->where($idColumn, $id);
    $this->db->update($table, $data);
    $update_id = $this->db->affected_rows();

    return $update_id;
}
foreach($key=>$value形式的数据){
$i=0;
如果($key'updatedby'&&&$key'updatedate'){
foreach($行的值){
$result=$this->myModel->recordUpdate($idColumn,$data['id'][$i],数组($key=>$row),$table);
$i=$i+1;
}
}
如果($key=='updatedby'){
foreach($data['id']作为$row){
$result=$this->myModel->recordUpdate($idColumn,$row,array($key=>$data['updatedby']),$table);
}
}
如果($key=='updateDate'){
foreach($data['id']作为$row){
$result=$this->myModel->recordUpdate($idColumn,$row,array($key=>$data['updatedate']),$table);
}
}
}
函数记录更新($idColumn,$id,$data,$table)
{
$this->db->where($idColumn,$id);
$this->db->update($table,$data);
$update_id=$this->db->受影响的_行();
返回$update\u id;
}

有没有办法先行后列进行主循环,我想让这段代码更简短、更可靠。

您可以先过滤和转置主数据:

$rows = array_transpose(array_filter($data, function($key){
    return !in_array($key, ["updatedby","updateddate"]);
}, ARRAY_FILTER_USE_KEY));

function array_transpose($array){
    $keys = array_keys($array);
    $new_array = [];
    for($i = 0, $len = count($array[$keys[0]]); $i < $len; $i++){
        $new_array[$i] = [];
        foreach($keys as $key){
            $new_array[$i][$key] = $array[$key][$i];
        }
    }
    return $new_array;
}
$rows=array\u转置(array\u过滤器($data,function($key)){
return!在数组中($key,[“updatedby”,“updatedate]”);
},数组_FILTER_USE_KEY));
函数数组\转置($array){
$keys=数组\ U键($array);
$new_数组=[];
对于($i=0,$len=count($array[$keys[0]]);$i<$len;$i++){
$new_数组[$i]=[];
foreach($key作为$key){
$new_数组[$i][$key]=$array[$key][$i];
}
}
返回$new_数组;
}
然后可以像这样在行上循环:

for($row = 0, $cnt = count($rows); $row < $cnt; $row++){
    $column_data = $rows[$row];
    $column_keys = array_keys($column_data);
    $column_id = $column_data[$idColumn];
    foreach($column_keys as $key){
       recordUpdate(
           $idColumn,
           $column_id,
           [$key => $column_data[$key]],
           $table
       );
   }

   // set the updatedby and updateddate for each row
   $this->myModel->recordUpdate($idColumn, $column_id, ["updatedby" => $data["updatedby"]], $table);
   $this->myModel->recordUpdate($idColumn, $column_id, ["updateddate" => $data["updateddate"]], $table);
}
for($row=0,$cnt=count($rows);$row<$cnt;$row++){
$column_data=$rows[$row];
$column\u keys=数组\u keys($column\u data);
$column_id=$column_data[$idColumn];
foreach($column\u key作为$key){
记录更新(
$idColumn,
$column_id,
[$key=>$column_data[$key]],
$table
);
}
//为每行设置updatedby和UpdateDate
$this->myModel->recordUpdate($idColumn,$column_id,[“updatedby”=>$data[“updatedby”]],$table);
$this->myModel->recordUpdate($idColumn,$column_id,[“updatedate”=>$data[“updatedate”]],$table);
}

I在您的最后一行[“updatedby”=>$data[“updatedby”]到[“updatedby”=>$data[“updatedby”]]的括号中固定了以下错误:消息:未定义变量:键码不起作用:消息:未定义偏移量:您代码$column\u data=$rows[$row]的第4行上的0;我正在调试,完成后将在此处写入。$row_count应该是count($rows[$idColumn])-1对吗?更新了答案。请注意,转置函数不包含任何检查。