99网
您的当前位置:首页laravel中DB查询数据库后,返回的对象转为数组

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

来源:99网
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]);

因篇幅问题不能全部显示,请点此查看更多更全内容