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]);


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM