Viewed   73 times

So I have been searching for a while and cannot find the answer to a simple question. Is it possible to have an array of objects in PHP? Such as:


For some reason I have not been able to find the answer anywhere. I assume it is possible but I just need to make sure.



The best place to find answers to general (and somewhat easy questions) such as this is to read up on PHP docs. Specifically in your case you can read more on objects. You can store stdObject and instantiated objects within an array. In fact, there is a process known as 'hydration' which populates the member variables of an object with values from a database row, then the object is stored in an array (possibly with other objects) and returned to the calling code for access.

-- Edit --

class Car
    public $color;
    public $type;

$myCar = new Car();
$myCar->color = 'red';
$myCar->type = 'sedan';

$yourCar = new Car();
$yourCar->color = 'blue';
$yourCar->type = 'suv';

$cars = array($myCar, $yourCar);

foreach ($cars as $car) {
    echo 'This car is a ' . $car->color . ' ' . $car->type . "n";
Saturday, December 24, 2022

You can do

function unsetValue(array $array, $value, $strict = TRUE)
    if(($key = array_search($value, $array, $strict)) !== FALSE) {
    return $array;

You can also use spl_object_hash to create a hash for the objects and use that as array key.

However, PHP also has a native Data Structure for Object collections with SplObjectStorage:

$a = new StdClass; $a->id = 1;
$b = new StdClass; $b->id = 2;
$c = new StdClass; $c->id = 3;

$storage = new SplObjectStorage;
echo $storage->count(); // 3

// trying to attach same object again
echo $storage->count(); // still 3

var_dump( $storage->contains($b) ); // TRUE
var_dump( $storage->contains($b) ); // FALSE

SplObjectStorage is Traversable, so you can foreach over it as well.

On a sidenote, PHP also has native interfaces for Subject and Observer.

Tuesday, August 30, 2022

As order of values in array returned by Object.values() isn't guaranteed, you should consider use of .map() with some Object Destructuring. You can then extract object properties in separate variables and return them in desired order explicitly.

const data = [
  { amount: '100', user: 'admin', date: 'March 6, 2019' },
  { amount: '120', user: 'admin', date: 'March 6, 2019' },
  { amount: '80',  user: 'admin', date: 'March 7, 2019' },
  { amount: '200', user: 'admin', date: 'March 7, 2019' }

const result ={ amount, user, date }) => [amount, user, date]);

.as-console-wrapper { max-height: 100% !important; top: 0; }
Wednesday, August 3, 2022

Like this:

foreach($books as $book)
    $categories[$book->category][] = $book;
Sunday, August 28, 2022
function benchmark($func_name, $iterations) {
    $begin = microtime(true);
    for ($i = 0; $i < $iterations; $i++) {
    $end = microtime(true);

    $execution_time = $end - $begin;

    echo $func_name , ': ' , $execution_time;

function standClass() {
    $obj = new stdClass();
    $obj->param_one = 1;
    $obj->param_two = 2;

function castFromArray() {
    $obj = (object)array('param_one' => 1, 'param_two' => 2);

benchmark('standClass', 1000);
benchmark('castFromArray', 1000);

benchmark('standClass', 100000);
benchmark('castFromArray', 100000);


standClass: 0.0045979022979736
castFromArray: 0.0053138732910156

standClass: 0.27266097068787
castFromArray: 0.20209217071533

Casting from an array to stdClass on the fly is around 30% more efficient, but the difference is still negligible until you know you will be performing the operation 100,000 times (and even then, you're only looking at a tenth of a second, at least on my machine).

So, in short, it doesn't really matter the vast majority of the time, but if it does, define the array in a single command and then type-cast it to an object. I definitely wouldn't spend time worrying about it unless you've identified the code in question as a bottleneck (and even then, focus on reducing your number of iterations if possible).

Saturday, December 3, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :