. Advertisement .
..3..
. Advertisement .
..4..
Are you looking for a way to create array of PHP objects? You are at the right place! This article will show you how to do so with different methods. Check it out and follow along to complete your task.
Method 1: Create Array Of PHP Class’ Objects
In PHP, you may create an object array using the array()
method. It will generate an array of the objects using the arguments as input.
You can create the objects by developing a class and specifying some of its properties. The class’s properties will each have a specific value. These values and properties will finally combine to create a KVP (key-value pair) in the array.
For instance, make a Motorcycle class. Then, create $style
and $name
as the properties. Next, create an object $bike1
of the class Motorcycle utilizing the keyword new. Add any acceptable values to the object’s properties.
The second object, $bike2
, should be created similarly and set its values appropriately. Make a variable called $bike
, then write the function array()
to it containing the $bike1
object and $bike2
object as parameters. The print_r()
method should then be used to print your $bikes
array variable.
As can be seen in the output area, you can generate an object array. In the example below, the array of the objects Motorcycle
has been constructed. You may see indices 1
and 0
for each object Motorcycle
. As mentioned before, each object’s values and properties are constructed as a KVP.
Here is the sample code:
<?php
class Motorcycle
{
public $name;
public $type;
}
$bike1 = new Motorcycle();
$bike1->name = 'Husqvarna';
$bike1->type = 'dirt';
$bike2 = new Motorcycle();
$bike2->name = 'Goldwing';
$bike2->type = 'touring';
$bikes = array($bike1, $bike2);
?>
<pre><?php print_r($bikes);?> </pre>
Output:
Array
(
[0] => Motorcycle Object
(
[name] => Husqvarna
[type] => dirt
)
[1] => Motorcycle Object
(
[name] => Goldwing
[type] => touring
)
)
Method 2: Create Array Of PHP Stdclass Objects
You may produce an object array by generating a PHP object of the stdClass. The object stdClass is declared in PHP’s standard function set. Instead of being an object’s base class, this class is empty and can be utilized for typecasting and setting dynamic properties.
Now you can create a stdClass object. After that, you can use the indexes to give the dynamic properties to this object.
For instance, use the keyword new to construct the $bikes[]
array as a stdClass’s object. Next, give the array $bikes[]
the 0 index and apply the properties type and name. Specify some reasonable values for the properties. Repeat this technique for the array $bikes[]
’s index 1. Then, print the array $bikes
.
As can be seen in the output part below, the following example generates the array of objects stdClass.
<?php
$bikes[] = new stdClass;
$bikes[0]->name = 'Husqvarna';
$bikes[0]->type = 'dirt';
$bikes[1]->name = 'Goldwing';
$bikes[1]->type = 'touring';
?>
<pre><?php print_r($bikes);?> </pre>
Output:
Array
(
[0] => stdClass Object
(
[name] => Husqvarna
[type] => dirt
)
[1] => stdClass Object
(
[name] => Goldwing
[type] => touring
)
)
Method 3: Array Of PHP Object Utilizing The Array() Feature
This approach resembles the first approach. By constructing objects from one class, you may produce an object array. In this case, you will utilize the array()
feature to build an array before populating it with the objects.
As in the first approach, objects were created and added to the array utilizing the array()
feature. The values for the properties will be set using the index of the array.
Here is an example. Make a class Motorcycle and give it the same properties as method 1. Next, employ the array()
method to generate an array utilizing the variable $bikes
. Let the array be empty. After that, use the index 0 to create a class object from the array $bike
.
Now set the values and properties for the index 0 as well. Follow the same procedure for index 1. Finally, utilize the print_r()
method to print your array.
You can use the following code as a reference.
<?php
class Motorcycle
{
public $name;
public $type;
}
$bikes = array();
$bikes[0] = new Motorcycle();
$bikes[0]->name = 'Husqvarna';
$bikes[0]->type = 'dirt';
$bikes[1] = new Motorcycle();
$bikes[1]->name = 'Goldwing';
$bikes[1]->type = 'touring';
?>
<pre><?php print_r($bikes);?> </pre>
Output:
Array
(
[0] => Motorcycle Object
(
[name] => Husqvarna
[type] => dirt
)
[1] => Motorcycle Object
(
[name] => Goldwing
[type] => touring
)
)
The Bottom Line
So there you have a complete tutorial on creating an array of PHP objects. This guide has provided three different methods with examples to make things clearer for you.PHP is an all-purpose scripting language which is particularly appropriate for web development. If you want to gain more skills in PHP, you can try to learn how to print console with this tutorial.
Leave a comment