I am integrating with an API and need to create objects to submit to the API server.
I could write the following
$objBedroom1 = $objRequest->property->details->rooms()->create();
$objBedroom1->room_name = "Bedroom1";
print_r($objBedroom1) ;
$objBedroom2 = $objRequest->property->details->rooms()->create();
$objBedroom2->room_name = "Bedroom2";
print_r($objBedroom2) ;
etc but teh bedrooms are not always set to 3, they could be anything up to 5 so I want to be able to iterate through the # of bedrooms and create an object for each but the issue is if I try the following...
$bedz = 3;//or set by the database variable as an int
for ($i=0; $i < $bedz; $i++) {
$objBedroom.$i = $objRequest->property->details->rooms()->create();
$objBedroom.$i->room_name = "Bedroom.$i";
print_r($objBedroom.$i) ;
}
The php error Undefined variable: objBedroom comes back
It would seem that I cant create a $objBedroom plus a variable number.
Any help appreciated
Regards Keith
An old dog trying to learn new tricks!
$objBedroom.$iisn't valid PHP. You could create an array of bedroom objects instead; e.g:$objBedroom = $objRequest->property->details->rooms()->create(); $objBedroom->room_name = 'foo'; $bedrooms[] = $objBedroom;My question first however is, what will you be doing with these objects - are you adding them to a parent object or something?