Jump to content

Create an array inside a new object.


Go to solution Solved by requinix,

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.
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.