Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
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
role
Admin
$json
I want result like
$json = {'id':5, 'name':'Hassan', 'role':'Admin'};
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
Add a comment
Try this
$object = json_decode($json); $object->role = 'Admin'; $json = json_encode($object)
Required, but never shown
By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.
Start asking to get answers
Find the answer to your question by asking.
Explore related questions
See similar questions with these tags.