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

Afram's avatar
Level 1

Php (Push two object to an array)

i have an array that contain a name like that 'name' => 'user' so what i would to do is that i want to push two object to the same array, i want that array look like that ...

['name' => 'user'
'id' => 2
'role' => 'admin']

I have to do it this way because I get the id and the role from another table.

my code:

        
        $calender_config_info = auth()->user()->business->calender_config;
        
        $id = auth()->user()->business->id;

        $employeeInfo = Employee::where('business_id', $id)->get();
        

        
        foreach($employeeInfo as $info){
            $employeeInfo = array(
                'id' => $info['id'],
                'role' => $info['role'],
            );
        }
        //dd($employeeInfo);

        $employee_list = [];
        foreach ($calender_config_info as $info) {
            $employee = array(
                'name' => $info['info']['Employee']
            );
            
            array_push($employee_list, $employee);
        }

        //dd($employee_list);

so i have the values but i don't know how to push it in the right way.
0 likes
2 replies
tykus's avatar
tykus
Best Answer
Level 104

The simple answer is array_merge to put your two associative arrays together

However, you surely can make a single query with a join?

1 like
Afram's avatar
Level 1

THANK YOU, I used array_merge that was so simple, I do not know how I could not find this answer. :( I will check a little how join also works. Thx again!

Please or to participate in this conversation.