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: Show
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 --
answered Dec 23, 2011 at 4:35
Mike PurcellMike Purcell 19.5k10 gold badges51 silver badges86 bronze badges 5 Yes.
Results in:
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.
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.
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:
Result:
answered Aug 16, 2021 at 22:53
SlipstreamSlipstream 11.9k3 gold badges54 silver badges43 bronze badges Another intuitive solution could be:
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.
|