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

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

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

DB查询数据库

Copy Highlighter-hljs
$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查询出来的数据

Copy Highlighter-hljs
$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 @   caibaotimes  阅读(2106)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
点击右上角即可分享
微信分享提示