How do you object an array in php?

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:

$ar=array[];    
$ar[]=$Obj1    
$ar[]=$obj2

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.

Rich Adams

25.4k4 gold badges39 silver badges62 bronze badges

asked Dec 23, 2011 at 4:17

0

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";
}

answered Dec 23, 2011 at 4:35

Mike PurcellMike Purcell

19.5k10 gold badges51 silver badges86 bronze badges

5

Yes.

$array[] = new stdClass;
$array[] = new stdClass;

print_r[$array];

Results in:

Array
[
    [0] => stdClass Object
        [
        ]

    [1] => stdClass Object
        [
        ]

]

answered Dec 23, 2011 at 4:21

ceejayozceejayoz

173k40 gold badges288 silver badges358 bronze badges

5

Yes, its possible to have array of objects in PHP.

class MyObject {
  private $property;

  public function  __construct[$property] {
    $this->Property = $property;
  }
}
$ListOfObjects[] = new myObject[1]; 
$ListOfObjects[] = new myObject[2]; 
$ListOfObjects[] = new myObject[3]; 
$ListOfObjects[] = new myObject[4]; 

print "
";
print_r[$ListOfObjects];
print "
";

KV Prajapati

92.6k19 gold badges144 silver badges183 bronze badges

answered Dec 26, 2012 at 3:21

rizonrizon

8,0411 gold badge26 silver badges17 bronze badges

2

Arrays can hold pointers so when I want an array of objects I do that.

$a = array[];
$o = new Whatever_Class[];
$a[] = &$o;
print_r[$a];

This will show that the object is referenced and accessible through the array.

answered Dec 4, 2012 at 21:57

LangelLangel

3154 silver badges7 bronze badges

You can do something like this:

$posts = array[
  [object] [
    'title' => 'title 1',
    'color' => 'green'
  ],
  [object] [
    'title' => 'title 2',
    'color' => 'yellow'
   ],
   [object] [
     'title' => 'title 3',
     'color' => 'red'
   ]
];

Result:

var_dump[$posts];

array[3] {
  [0]=>
  object[stdClass]#1 [2] {
    ["title"]=>
    string[7] "title 1"
    ["color"]=>
    string[5] "green"
  }
  [1]=>
  object[stdClass]#2 [2] {
    ["title"]=>
    string[7] "title 2"
    ["color"]=>
    string[6] "yellow"
  }
  [2]=>
  object[stdClass]#3 [2] {
    ["title"]=>
    string[7] "title 3"
    ["color"]=>
    string[3] "red"
  }
}

answered Aug 16, 2021 at 22:53

SlipstreamSlipstream

11.9k3 gold badges54 silver badges43 bronze badges

Another intuitive solution could be:

class Post
{
    public $title;
    public $date;
}

$posts = array[];

$posts[0] = new Post[];
$posts[0]->title = 'post sample 1';
$posts[0]->date = '1/1/2021';

$posts[1] = new Post[];
$posts[1]->title = 'post sample 2';
$posts[1]->date = '2/2/2021';

foreach [$posts as $post] {
  echo 'Post Title:' . $post->title . ' Post Date:' . $post->date . "\n";
}

answered Feb 25, 2021 at 16:03

1

Although all the answers given are correct, in fact they do not completely answer the question which was about using the [] construct and more generally filling the array with objects.

A more relevant answer can be found in how to build arrays of objects in PHP without specifying an index number? which clearly shows how to solve the problem.

answered Dec 6, 2019 at 19:42

FiboFibo

317 bronze badges

How do you object in PHP?

Define Objects Classes are nothing without objects! We can create multiple objects from a class. Each object has all the properties and methods defined in the class, but they will have different property values. Objects of a class is created using the new keyword.

How can we store objects in array in PHP?

Method 2: Type Casting object to an array: Typecasting is the way to utilize one data type variable into the different data type and it is simply the explicit conversion of a data type. It can convert a PHP object to an array by using typecasting rules supported in PHP.

What is difference between array and object in PHP?

Objects represent a special data type that is mutable and can be used to store a collection of data [rather than just a single value]. Arrays are a special type of variable that is also mutable and can also be used to store a list of values.

What is stdClass object in PHP?

The stdClass is the empty class in PHP which is used to cast other types to object. It is similar to Java or Python object. The stdClass is not the base class of the objects. If an object is converted to object, it is not modified.

Chủ Đề