PHP——格式化数据引发的思考

随便记录下,直接用传过来的list造成没有进行格式化处理$list[$k]['sex']=config(...);发生错误,大佬修改为$item=$v;然后用完即销毁,记录下,回头详细更

/**
     * 根据查询结果,格式化数据
     * @param $list
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function tableFormat($list)
    {
        $result = [];
        foreach ($list as $k => $v) {
            $item = $v;
            if ($v['sex']) {
                $item['sex']  = config('params.user')['sex'][$v['sex']];
            }
            if ($v['status']) {
                $item['status'] = config('params.user')['status'][$v['status']];
            }
            if ($v['ctime']) {
                $item['ctime'] = getTime($v['ctime']);
            }
            if ($v['ltime']) {
                $item['ltime'] = getTime($v['ltime']);
            }
            if ($v['utime']) {
                $item['utime'] = getTime($v['utime']);
            }
            if (isset($v['avatar']) && $v['avatar']) {
                $item['avatar'] = _sImage($v['avatar']);
            }
            $result[] = $item;
            unset($item);
        }
        
        return $result;
    }

猜你喜欢

转载自www.cnblogs.com/wangyang0210/p/10521194.html