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);
}
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b3b558-34651.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b3b558-34651.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?