ttmt_101 Posted July 10, 2013 Share Posted July 10, 2013 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 More sharing options...
requinix Posted July 10, 2013 Share Posted July 10, 2013 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. Link to comment https://forums.phpfreaks.com/topic/280045-create-an-array-inside-a-new-object/#findComment-1440228 Share on other sites More sharing options...
ttmt_101 Posted July 10, 2013 Author Share Posted July 10, 2013 so my example should be 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/#findComment-1440235 Share on other sites More sharing options...
requinix Posted July 10, 2013 Share Posted July 10, 2013 Yes. Link to comment https://forums.phpfreaks.com/topic/280045-create-an-array-inside-a-new-object/#findComment-1440236 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.