第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

求教一個(gè)素組問題

求教一個(gè)素組問題

PHP
紅顏莎娜 2019-02-04 00:00:22
$arr=[            'id'=>[                 1,                 2             ],            'temp'=>[                'x','y'             ],            'user'=>[                 'b','f'             ]          ];                  這樣的數(shù)組如何生成兩條update的sql語句        update set temp=x ,user=b where id=1        update set temp=y,user=f where id=2
查看完整描述

1 回答

?
皈依舞

TA貢獻(xiàn)1851條經(jīng)驗(yàn) 獲得超3個(gè)贊

<?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);

結(jié)果

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"}

如果使用 ORM 框架更新數(shù)據(jù)例如Article::where($whereKey, $whereValue)->update($updateArr);,可以改為

<?php$arr=[    'id'=>[        1,        2
    ],    'temp'=>[        'x','y'
    ],    'user'=>[         'b','f'
    ]
];// 數(shù)組第一個(gè)元素是 where 條件, 其余的所有元素都是更新對(duì)象$whereKey = key($arr);// 獲取 where 條件并從數(shù)組去掉$whereParams = array_shift($arr);

$updateKeys = array_keys($arr);foreach ($whereParams as $index => $whereValue) {
    $updateArr = [];    foreach ($updateKeys as $updateKey) {
        $updateArr[$updateKey] = $arr[$updateKey][$index];
    }


查看完整回答
反對(duì) 回復(fù) 2019-02-04
  • 1 回答
  • 0 關(guān)注
  • 488 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)