In PHP, How to Add an Object Element to an Array

In PHP, how can I add an object element to an array?

Just do:

$object = new stdClass();
$object->name = "My name";
$myArray[] = $object;

You need to create the object first (the new line) and then push it onto the end of the array (the [] line).

You can also do this:

$myArray[] = (object) ['name' => 'My name'];

However I would argue that's not as readable, even if it is more succinct.

Add objects to an array

Right now you're just reassigning $discs[123] to red from blue.

To Add to, add a new secondary key

$discs[123][0] = new Disc("blue");
$discs[123][1] = new Disc("red");

Creating a multidimensional array

Adding value to array inside object PHP

You cann't use array_push this way. $object_name is not your main object.

When you push to $object_name, your $myobject is still empty.

You can fix it adding reference &, for example:

$object_name = &$myobject->name;

or just push to your original object:

array_push($myobject->name, "testName");

or

$myobject->name[] = "something";

How to add elements to an array that is a property of an object in PHP?

You need to do 2 changes:

1.Use $this->passengers inside function.

2.Use print_r() to print it

<?php

class Car {
public $model;
public $year;
public $passengers;

function __construct() {
$this->model = "";
$this->year = " ";
$this->passengers=array();
}

function addPassengers($passenger)
{
array_push($this->passengers, $passenger);
return $this->passengers;
}
}

$herbie = new Car();
$herbie->model = "vw";
$herbie->year = "1997";
$herbie->addPassengers("Mike");

echo $herbie->model;
echo $herbie->year;
print_r($herbie->passengers);
?>

Output:- https://3v4l.org/aQn2h

Note:- You can write less amount of code and get the same output: https://3v4l.org/KTVAp

As other comments stated you need to use private variables instead of public

Sample example: -https://3v4l.org/8RF9j

PHP - adding an object with its properties to array

PHP returns arrays as a value instead of as a reference. This means you must set the modified value back somehow.

Looking at the library apparently in question, there seems to be setExcludedLocations method for that purpose.

So your code should be something like:

$geo_targeting = $lineItem->getTargeting()->getGeoTargeting();
$excluded_locations = $geo_targeting->getExcludedLocations();
array_push($excluded_locations, $location);
$geo_targeting->setExcludedLocations($excluded_locations);

PHP prepend object in array of objects

You should be able to "simply" merge the two arrays:

$post_types = array_merge($prepend, $post_types);

How do I work with an array object in PHP?

There is no need to json_encode the data. Since the data is an instance of Laravel Collection, you can manipulate it like so

$item = $data->firstWhere('label', '1mm+'); // get the item
$data = $data->filter(fn($value, $key) => $value->label !== '1mm+') // remove $item from $data
->push($item); // move $item to the end of data

How to put elements in an object array in php?

Each time you run new DNA creates a single object, so if you want to have multiple objects you need to call it multiple times.

In your case, you're just running it once: $dna1[] = new DNA; creates a single object, and adds it to an array.

To create three objects, you could do this:

// First create an empty array
$dna1 = [];

// Now add objects to it one by one
$dna1[0] = new DNA;
$dna1[0]->setRSID(1);
$dna1[0]->setCHROMOSOME(2);

$dna1[1] = new DNA;
$dna1[1]->setRSID(5);
$dna1[1]->setCHROMOSOME(3);

$dna1[2] = new DNA;
$dna1[2]->setRSID(7);
$dna1[2]->setCHROMOSOME(0);


Related Topics



Leave a reply



Submit