Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

babo's avatar
Level 1

How to convert an array to object in PHP

Hi everyone, i have a probrem for convert array to object, but the format is still an array in an array

class ReportExport implements FromCollection
{
public function collection()
    {
      $cross_kehadiran[] = DB::table('attendance as in')
                        ->where('in.in_out', 'in')
                        ->where('in.attendance_location_id', $locations->id)
                        ->where('in.company_id', '!=', \Session::get('selected_company'))
                        ->whereDate('in.created', Carbon::today())
                        ->leftJoin('attendance as out', function ($join) {
                            $join->on('in.employee_id', 'out.employee_id')
                                ->where('out.in_out', 'out')
                                ->where('out.attendance_location_id', 'in.attendance_location_id')
                                ->whereDate('out.created', Carbon::today());
                        })
                        ->join('employee', 'employee.id', 'in.employee_id')
                        ->join('location_library', 'location_library.id', 'in.attendance_location_id')
                        ->join('company as cp', 'cp.id', 'in.company_id')
                        ->join('employee_in_app as e_app', 'e_app.employee_id', 'in.employee_id')
                        ->select('employee.name', 'cp.alias', 'in.employee_id','location_library.location_name',  DB::raw('DATE_FORMAT(in.attendance_time, "%H:%i:%s") as in_time'), DB::raw('DATE_FORMAT(out.attendance_time, "%H:%i:%s") as out_time'), 'e_app.note')
                        ->orderBy('in.attendance_time', 'DESC')
                        ->get();

     $object = (object) $cross_kehadiran;

     dd($object);
  }
}
0 likes
2 replies
Sinnbeck's avatar

You need an array of key value pairs

But you can just use the db query to get a collection, which I assume is what you actually want

$collection = DB::table('attendance as in')
                        ->where('in.in_out', 'in')
                        ->where('in.attendance_location_id', $locations->id)
                        ->where('in.company_id', '!=', \Session::get('selected_company'))
                        ->whereDate('in.created', Carbon::today())
                        ->leftJoin('attendance as out', function ($join) {
                            $join->on('in.employee_id', 'out.employee_id')
                                ->where('out.in_out', 'out')
                                ->where('out.attendance_location_id', 'in.attendance_location_id')
                                ->whereDate('out.created', Carbon::today());
                        })
                        ->join('employee', 'employee.id', 'in.employee_id')
                        ->join('location_library', 'location_library.id', 'in.attendance_location_id')
                        ->join('company as cp', 'cp.id', 'in.company_id')
                        ->join('employee_in_app as e_app', 'e_app.employee_id', 'in.employee_id')
                        ->select('employee.name', 'cp.alias', 'in.employee_id','location_library.location_name',  DB::raw('DATE_FORMAT(in.attendance_time, "%H:%i:%s") as in_time'), DB::raw('DATE_FORMAT(out.attendance_time, "%H:%i:%s") as out_time'), 'e_app.note')
                        ->orderBy('in.attendance_time', 'DESC')
                        ->get();
dd($collection);
    
johnDoe220's avatar

In the simplest case, it's probably sufficient to "cast" the array as an object:

$object = (object) $array;

Another option would be to instantiate a standard class as a variable, and loop through your array while re-assigning the values:

$object = new stdClass();
foreach ($array as $key => $value)
{
    $object->$key = $value;
}
1 like

Please or to participate in this conversation.