Jump to content

Create an array inside a new object.


ttmt_101

Recommended Posts

Hi all

 

Is it possible to create an array in an object and then add an array to that array

 

Something like this. This is completely nonsense code, I'm just trying to illustrate my question.

 



class Car{
var $car_list = array();
}

$cars = array('ford','nissan','renault');

$my_car = new Car;

array_push($my_car->$car_list,$cars)

Link to comment
https://forums.phpfreaks.com/topic/280045-create-an-array-inside-a-new-object/
Share on other sites

Except for the extra $ in ->$car_list you have it right.

 

Then car_list will be an array containing an array of the cars.

Car Object
(
    [car_list] => Array
        (
            [0] => Array
                (
                    [0] => ford
                    [1] => nissan
                    [2] => renault
                )

        )

)
If you want to "merge" the arrays together then array_merge them.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.