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) Quote 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. Quote 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) Quote 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...
Solution requinix Posted July 10, 2013 Solution Share Posted July 10, 2013 Yes. Quote 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
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.