Ask a prime group problem

$arr=[
            "id"=>[
                1,
                2
            ],
            "temp"=>[
                "x","y"
            ],
            "user"=>[
                 "b","f"
            ]
         ];
         
       updatesql
       update set temp=x ,user=b where id=1
       update set temp=y,user=f where id=2
Php
May.07,2022

<?php
$arr=[
    'id'=>[
        1,
        2
    ],
    'temp'=>[
        'x','y'
    ],
    'user'=>[
         'b','f'
    ]
];
$sqls = [];
$whereKey = key($arr);
$whereParams = array_shift($arr);
$updateKeys = array_keys($arr);
foreach ($whereParams as $index => $whereValue) {
    $updateSqlArr = array_map(function($updateKey) use ($index, $arr) {
        return $updateKey . ' = ' . $arr[$updateKey][$index];
    }, $updateKeys);

    $sqls[] = 'update tablename set ' . implode(', ', $updateSqlArr) . ' where ' . $whereKey . ' = ' . $whereValue;
}
var_dump($sqls);

result

array(2) {
  [0]=>
  string(52) "update tablename set temp = x, user = b where id = 1"
  [1]=>
  string(52) "update tablename set temp = y, user = f where id = 2"
}

if you use the ORM framework to update data such as Article::where ($whereKey, $whereValue)-> update ($updateArr); , you can change to

<?php
$arr=[
    'id'=>[
        1,
        2
    ],
    'temp'=>[
        'x','y'
    ],
    'user'=>[
         'b','f'
    ]
];
//  where , 
$whereKey = key($arr);
//  where 
$whereParams = array_shift($arr);

$updateKeys = array_keys($arr);
foreach ($whereParams as $index => $whereValue) {
    $updateArr = [];
    foreach ($updateKeys as $updateKey) {
        $updateArr[$updateKey] = $arr[$updateKey][$index];
    }
    Article::where($whereKey, $whereValue)->update($updateArr);
}
Menu