Home > OS >  Combine two json arrays to one in Laravel php
Combine two json arrays to one in Laravel php

Time:10-01

{"device_id":null,"collector_id":2}
{"id":1,"name":"hello","email":"[email protected]","location":null,"latitude":null,"longitude":null}

I want to combine these two to one

{"id":1,"name":"hello","email":"[email protected]","location":null,"latitude":null,"longitude":null,"device_id":null,"collector_id":2}

How can I do that?

CodePudding user response:

In Javascript you can do so:

const obj1 = {"device_id":null,"collector_id":2};
const obj2 = {"id":1,"name":"hello","email":"[email protected]","location":null,"latitude":null,"longitude":null};
let result = Object.assign(obj1, obj2);
  1. Spread syntax:
let result = {...obj1, ...obj2};

CodePudding user response:

Here's how: check out json_decode and array_merge functions in PHP

  • Related