laravel中DB查询数据库后,返回的对象转为数组

先编码成json字符串,再解码成数组

/**
 * [objectToArray 先编码成json字符串,再解码成数组]
 * @param  [type] $object [description]
 * @return [type]         [description]
 */
public function objectToArray($object) {
    return json_decode(json_encode($object), true);
}

DB查询数据库

$res = DB::connection('mysql_crm')->table('yu_school_ersanshi')->get()->map(function ($value){
            return (array)$value;
        });

return gettype($res[0]);
//return $res[0]['id'];

原生sql查询出来的数据

$sql = "select a.zwmc,a.id,a.szgj from gwdxyxk as a left join yu_school_ersanshi as b on a.id=b.s_ys_id where a.szgj='a0920192AD18538XLtaQ' and b.content1 is null and a.zwmc is not null";
        
$res = DB::connection('mysql_crm')->select($sql);

$a = array_map(function ($value){
    return (array)$value;
},$res);

return $a[0];
//return gettype($a[0]);
posted @ 2021-01-15 16:06  caibaotimes  阅读(2081)  评论(0编辑  收藏  举报