Php array with multiple objects

First, that:

$rArray = array[
            $rData,
            $rData2
          ];

It is not an object's concatenation, it's just an array's creation with two differents object like that:

$rArray = array[];    // create array
$rArray[] = $rData;   // add first object
$rArray[] = $rData2;  // add second object

Your code create one PHP array, contain two PHP objects [$rData and $rData2]; You can access to your object with the id of the array like that :

$rArray[0] // call the $rdata object
$rArray[1] // call the $rdata2 object

And for acces to the object properties :

 echo $rArray[0]->DateOfBirth // output : 1985-01-01

Or you can iterate on all the array for create an html Table [it's a sample]


Output of the foreach is :

john 1985-01-01
jane 1980-12-12

Do you have an other problem with PHP Array and object?

This is a short guide on how to work with a PHP array that contains objects. In this example, we will create multiple objects and then add them to a PHP array. Afterwards, we will loop through that array of objects and call one of their methods.

For the purposes of this post, I have created an example PHP class called Person:

class Person{

    //Person's name.
    public $name;

    //Object constructor allows us to set the Person's name.
    public function __construct[$name]{
        $this->name = $name;
    }

    //Object method that prints out "Hi!"
    public function sayHi[]{
        echo 'Hi, I\'m ' . $this->name;
    }
}

This PHP class above is pretty simple. It has an attribute called $name, which will contain the name of the Person and it has a function called “sayHi”, which prints out the object’s name to the browser.

Now, let’s create a few objects from this class and then add them to an array:

//Empty array.
$people = array[];

//Create three Person objects. Each with a different name.
//Add each Person object to our $people array.
$person1 = new Person['Dave'];
$people[] = $person1;

$person2 = new Person['Sarah'];
$people[] = $person2;

$person3 = new Person['Michael'];
$people[] = $person3;

In the PHP snippet above, we created three Person objects called Dave, Sarah and Michael. We then added each object to an array called $people.

Looping through an array of objects.

Now, let’s loop through that array and call the “sayHi” method on each object:

//Loop through our array of objects
//and call the sayHi method.
foreach[$people as $person]{
    $person->sayHi[];
}

If you run the PHP code above, you will see that we were able to loop through our objects and access them as we normally would. In this case, they each printed out their own individual name.

It’s that simple.

Hopefully, this post helped a little!

4.3.3. Discussion

In PHP, keys are unique per array, so you can't associate more than one entry in a key without overwriting the old value. Instead, store your values in an anonymous array:

$fruits['red'][ ] = 'strawberry';
$fruits['red'][ ] = 'apple';
$fruits['yellow'][ ] = 'banana';

Or, if you're processing items in a loop:

while [list[$color,$fruit] = mysql_fetch_array[$r]] {
    $fruits[$color][ ] = $fruit;
}

To print the entries, loop through the array:

foreach [$fruits as $color=>$color_fruit] {
    // $color_fruit is an array
    foreach [$color_fruit as $fruit] {
        print "$fruit is colored $color.
"; } }

Or use the pc_array_to_comma_string[ ] function from Recipe 4.10.

foreach [$fruits as $color=>$color_fruit] {
    print "$color colored fruits include " . 
        pc_array_to_comma_string[$color_fruit] . "
"; }

Object Initialization

To create a new object, use the new statement to instantiate a class:

For a full discussion, see the Classes and Objects chapter.

Converting to object

If an object is converted to an object, it is not modified. If a value of any other type is converted to an object, a new instance of the stdClass built-in class is created. If the value was null, the new instance will be empty. An array converts to an object with properties named by keys and corresponding values. Note that in this case before PHP 7.2.0 numeric keys have been inaccessible unless iterated.

For any other value, a member variable named scalar will contain the value.

helpful at stranger dot com

10 years ago

By far the easiest and correct way to instantiate an empty generic php object that you can then modify for whatever purpose you choose:



I had the most difficult time finding this, hopefully it will help someone else!

Anthony

6 years ago

In PHP 7 there are a few ways to create an empty object:



$obj1 and $obj3 are the same type, but $obj1 !== $obj3. Also, all three will json_encode[] to a simple JS object {}:



Outputs: [{},{},{}]

Ashley Dambra

8 years ago

Here a new updated version of 'stdObject' class. It's very useful when extends to controller on MVC design pattern, user can create it's own class.

Hope it help you.


works and displays:
stdClass Object
[
    [a] => A
    [b] => B
    [0] => C
]

But this:


or

Chủ Đề