Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add a new key value pair to an object in PHP?

I guess my question is two-fold

In Python if I do this

x = {key: value}

I've created a dictionary

In javaScript if I do the same thing I've created an object

What is that in PHP? It could just be that I don't know the terms

I'm working on a project with laravel. I basically have an sqlite database which I am accessing. This is returning an array of 'objects'(in javaScript terms). I want to do a foreach loop through the 'objects' in the array and I want to add a new key and value to the 'object'.

Secondly - Sorry I'm not 100 percent certain of the syntax- in php:

$person = [0 => {name: "Jake", age: 22},
           1 => {name: "Chris", age: 34}];

Again I'm really sorry about the syntax, I'm receiving the array directly from the sqlite database so it isn't overly important at this stage.

My question is - If I want to add say, a height to each person, how do I do that? Let me rephrase that, I know to loop through the array with a foreach, I just need the command which will allow me to append a new key/value pair to the end of a Dictionary/Object/What they are called in PHP.The result would be:

$person = [0 => {name: "Jake", age: 22, height: "2m"},
          1 => {name: "Chris", age: 34, height: "3m"}];

I know it is a super simple question but I'm struggling to find the right questions to ask google. If I am asking the wrong question please enlighten me I've been scratching my head for too long on something that I could do so easily in another language. I really appreciate the help.

like image 249
ObamoDank Avatar asked Jan 22 '26 20:01

ObamoDank


2 Answers

This will append the new object at the end of the object array

$objectArray[] = $newObject;

If you want to add new property to the object you can simply do this:

$object->newPropery = 'value';
like image 103
failedCoder Avatar answered Jan 25 '26 11:01

failedCoder


You have two options here. The first is just an array. Arrays in PHP are associative, so they can use strings as keys, not just numbers. You'd do something like this:

$people = [
 [
  'name' => 'Jane Smith',
  'age' => 20,
 ],
 [
  'name' => 'Mary Sue',
  'age' => 30
 ]
]

To add something to an array like this, you could just define a new key: $people[0]['height'] = 2;. You can also use a for loop (or a foreach with references) to add this to every person based on some formula if you wish.

This is somewhat in bad taste, however, as arrays make terrible data structures: there's no predefined information about what data can be in them, what can be done with that data, and so on. Instead, you'll want to generally declare a class, at the very least like this:

class Person
{
    public $name;
    public $age;
    public $height;
}

Then you could create a $person = new Person(); and set the values using the arrow syntax: $person->name = 'Jane Smith';. Ideally, you would also want a constructor to accept the initial values and setters/getters to make it possible to validate data being passed into the object rather than blindly accepting whatever values are being given, but that can come later.

These are your two choices. Generally, pick arrays for quick and dirty temporary data structures and pick objects for more complex and persistent data. As I cannot see what you're trying to do with this data, I cannot strictly say whether an array or an object is the best fit here.

like image 32
andOlga Avatar answered Jan 25 '26 10:01

andOlga



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!