Reputation: 310
How to add Array Object and Data to JSON in Laravel
$json = {'id':5, 'name':'Hassan'};
I want to add new object role
and value Admin
to $json
I want result like
$json = {'id':5, 'name':'Hassan', 'role':'Admin'};
Upvotes: 3
Views: 10000
Reputation: 8618
Try this
$object = json_decode($json);
$object->role = 'Admin';
$json = json_encode($object)
Upvotes: 3
Reputation: 35220
You could decode the JSON, add the values you want and then encode it back to JSON:
$data = json_decode($json, true);
$data['role'] = 'Admin';
$json = json_encode($json);
json_decode() Docs
json_encode() Docs
Upvotes: 6