array_reduce()函数用法

向用户自定义函数发送数组中的值,并返回一个字符串:
array_reduce(array,myfunction,initial)
参数 描述
array 必需。规定数组。
myfunction 必需。规定函数的名称。
initial 可选。规定发送到函数的初始值。

应用

<?php

function sum($carry, $item) {
    var_dump($carry, $item);
    $carry += $item;
    echo '<br><hr>';
    return $carry;
}

$a = array(1, 2, 3, 4, 5);

var_dump(array_reduce($a, 'sum', 10));  结果: int(10) int(1) int(11) int(2) int(13) int(3) int(16) int(4) int(20) int(5) int(25)

代码应用:

public static function changeLog($params, $batch = false, $default = [])
{
    try{
        if ($batch) {//是否批量插入
            $sql = array_reduce($params, function ($sql, $val) use ($default) {//第三个参数作为$sql默认值,$params参与接下来循环处理,结果累加到sql语句后,然后执行批量写入
                $val['Change_No'] = isset($val['Change_No']) ? $val['Change_No'] : (isset($default['Change_No']) ? $default['Change_No'] : '');
                $val['Change_Item'] = isset($val['Change_Item']) ? $val['Change_Item'] : (isset($default['Change_Item']) ? $default['Change_Item'] : '');
                $val['Change_Type'] = isset($val['Change_Type']) ? $val['Change_Type'] : (isset($default['Change_Type']) ? $default['Change_Type'] : '');
                $val['Table_Name'] = isset($val['Table_Name']) ? $val['Table_Name'] : (isset($default['Table_Name']) ? $default['Table_Name'] : '');
                $val['Menu_Name'] = isset($val['Menu_Name']) ? $val['Menu_Name'] : (isset($default['Menu_Name']) ? $default['Menu_Name'] : '');
                $val['Old_Value'] = str_replace('\\','',strval(isset($val['Old_Value']) ? $val['Old_Value'] : (isset($default['Old_Value']) ? $default['Old_Value'] : '')));
                $val['New_Value'] = str_replace('\\','',strval(isset($val['New_Value']) ? $val['New_Value'] : (isset($default['New_Value']) ? $default['New_Value'] : '')));

                if (isset($default['Created_User'])) {
                    $val['Created_User'] = $default['Created_User'];
                } else {
                    $val['Created_User'] = session('user.gh_oa');
                }
                if (isset($default['Created_User'])) {
                    $val['Created_Site'] = $default['Created_Site'];
                } else {
                    $val['Created_Site'] = session('user.bm');
                }
                $sql .= "('{$val['Change_No']}', '{$val['Change_Item']}', '{$val['Change_Type']}', '{$val['Table_Name']}', '{$val['Menu_Name']}', \"{$val['Old_Value']}\", \"{$val['New_Value']}\", '{$val['Created_User']}', '{$val['Created_Site']}'),";
                return $sql;
            }, "(这里是第三个参数)INSERT INTO dot_change_log (Change_No, Change_Item, Change_Type, `Table_Name`, Menu_Name, Old_Value, New_Value, Created_User, Created_Site) VALUES ");
            $count = Db::execute(trim($sql, ','));
        } else {//单条写入
            $params['Created_User'] = session('user.gh_oa');
            $params['Created_Site'] = session('user.bm');
            $count = Db::table('dot_change_log')->insert($params);
        }
        return $count;
    }catch (Exception $e){
        $serviceObj = new BaseService();
        $serviceObj->record_exception_log('INMS_SYS_LOGISTICS_CHANGE_LOG',json_encode($params,JSON_UNESCAPED_UNICODE),$e->getMessage());
    }

}

发表评论

电子邮件地址不会被公开。 必填项已用*标注