Table of Contents
An array can contain a sequence of elements of different data types. From strings to integers to floats to objects.
In this post, we will discuss the different ways to how to create an array of objects in PHP.
Using []
syntax
We can use the short array syntax – []
– to create an array of objects by passing the object variable names to it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<?php $obj1 = (object) [ 'key1' => 'pair', 'key2' => 'pair', 'key3' => 'pair' ]; $obj2 = (object) [ 'key1' => 'pair', 'key2' => 'pair', 'key3' => 'pair' ]; $myArray = [$obj1, $obj2]; var_dump($myArray); |
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
****array(2) { [0]=> object(stdClass)#1 (3) { ["key1"]=> string(4) "pair" ["key2"]=> string(4) "pair" ["key3"]=> string(4) "pair" } [1]=> object(stdClass)#2 (3) { ["key1"]=> string(4) "pair" ["key2"]=> string(4) "pair" ["key3"]=> string(4) "pair" } } |
Using array()
method
We can simply use the array
method to create an array of objects. All we need to do is simply pass objects as its arguments.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<?php $obj1 = (object) [ 'key1' => 'pair', 'key2' => 'pair', 'key3' => 'pair' ]; $obj2 = (object) [ 'key1' => 'pair', 'key2' => 'pair', 'key3' => 'pair' ]; $myArray = array($obj1, $obj2); var_dump($myArray); ?> |
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
array(2) { [0]=> object(stdClass)#1 (3) { ["key1"]=> string(4) "pair" ["key2"]=> string(4) "pair" ["key3"]=> string(4) "pair" } [1]=> object(stdClass)#2 (3) { ["key1"]=> string(4) "pair" ["key2"]=> string(4) "pair" ["key3"]=> string(4) "pair" } } |
We can also use this same approach for class objects. By developing a class and specifying some of its features, we can produce objects. There will be some values for the class’s properties. The properties and values will finally combine to create a key-value pair in the array.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<?php class Student { public $name; public $id; } $std1 = new Student(); $std1->name = "Jacob"; $std1->id = "2015/19"; $std2 = new Student(); $std2->name = "Femi"; $std2->id = "2015/24"; $myArray = array($std1, $std2); var_dump($myArray); ?> |
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
array(2) { [0]=> object(Student)#1 (2) { ["name"]=> string(5) "Jacob" ["id"]=> string(7) "2015/19" } [1]=> object(Student)#2 (2) { ["name"]=> string(4) "Femi" ["id"]=> string(7) "2015/24" } } |
Here, we created Student class with 2 attributes, name
and id
.
Then created multiple objects and used array()
to create array of objects in PHP.
Using stdClass
Object
Another approach is to use of the stdClass
object. By generating a PHP object of the stdClass
, we may produce an array of objects. The PHP standard library of functions contains a definition for the stdClass
. It is an empty class that can be used to typecast and set dynamic properties rather than being a base class for objects.
Here is an example of how to create an array of objects in PHP using the stdClass
object:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
<?php // Create an empty array of objects $objects = array(); // Create a new stdClass object and set its properties $stdOne = new stdClass(); $stdOne->name = 'John'; $stdOne->age = 30; // Add the object to the array $objects[] = $stdOne; // Create another stdClass object and set its properties $stdTwo = new stdClass(); $stdTwo->name = 'Jane'; $stdTwo->age = 25; // Add the object to the array $objects[] = $stdTwo; // Print the array of objects print_r($objects); ?> |
This code will create an array of two objects, each of which has a name
and age
property. The resulting array will look something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
Array ( [0] => stdClass Object ( [name] => John [age] => 30 ) [1] => stdClass Object ( [name] => Jane [age] => 25 ) ) |
In this example, we create two stdClass
objects and set their properties individually. Then, we add each object to an array using the []
syntax, which appends the new value to the end of the array. Finally, we use the print_r
function to print the resulting array of objects.
That’s all about how to create array of Objects in PHP.