John Davidson

php - how to add another object with value in array

0 comments
Message:


How to add another object with value in data?


Something start like :


$data=[{name:"apple"}]

And i wanted output like this


$data=[{name:"apple",city:"gotham"}]


My Answer:
To add another object with a value in an array in PHP, you can use the following code:

php
$data = [
['name' => 'John'],
['name' => 'Jane']
];

$newObject = ['name' => 'Alice'];

$data[] = $newObject;

print_r($data);


This code will add a new object with the name 'Alice' to the existing array `$data`. The `print_r($data)` function will display the updated array with the new object included.

Rate this post

5 of 5 based on 9288 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us