rbaez Posted January 16, 2013 Share Posted January 16, 2013 Hello guys i want to know how can i iterate this array. stdClass Object ( [new_occ] => Array ( [0] => Array # This is the room number ( [adults] => 2 [children] => Array ( [0] => 0 [1] => 0 [2] => 0 ) ) [1] => Array ( [adults] => 1 ) [2] => Array ( [adults] => 2 [children] => Array ( [0] => 1 [1] => 1 [2] => 1 ) ) ) ) i want to print each of the elements but for adults and children, i only want the sum of the elements for each room. Thanks in advance. Quote Link to comment Share on other sites More sharing options...
requinix Posted January 16, 2013 Share Posted January 16, 2013 foreach ($foo->new_occ as $room => $occupants) { $adults = (isset($occupants["adults"]) ? $occupants["adults"] : 0); $children = (isset($occupants["children"]) ? array_sum($occupants["children"]) : 0); echo "Room {$room} has ", $adults + $children, "occupants\n"; } Quote Link to comment Share on other sites More sharing options...
rbaez Posted January 16, 2013 Author Share Posted January 16, 2013 foreach ($foo->new_occ as $room => $occupants) { $adults = (isset($occupants["adults"]) ? $occupants["adults"] : 0); $children = (isset($occupants["children"]) ? array_sum($occupants["children"]) : 0); echo "Room {$room} has ", $adults + $children, "occupants\n"; } Thanks dude, this really help me a lot. Just one more thing, how can i make than instead of room 0, room 1 and room 2 says room 1, room 2, room 3 Best Regards. Quote Link to comment Share on other sites More sharing options...
rbaez Posted January 16, 2013 Author Share Posted January 16, 2013 Thanks dude, this really help me a lot. Just one more thing, how can i make than instead of room 0, room 1 and room 2 says room 1, room 2, room 3 Best Regards. What i need to print is something like this: $new_room_occ .= ' <tr> <td>'.$room.'</td> # if its room 1 or room 2 or room 3 <td class="tc">'.$adults.'</td> # adult number <td>'.$children.'</td> # children number </tr> '; Best Regards. Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 16, 2013 Share Posted January 16, 2013 Your first post says: [0] => Array # This is the room number So either you have a room # 0 or you've messed up when structuring your array. Quote Link to comment Share on other sites More sharing options...
rbaez Posted January 16, 2013 Author Share Posted January 16, 2013 foreach ($foo->new_occ as $room => $occupants) { $adults = (isset($occupants["adults"]) ? $occupants["adults"] : 0); $children = (isset($occupants["children"]) ? array_sum($occupants["children"]) : 0); echo "Room {$room} has ", $adults + $children, "occupants\n"; } Hey man thank you very much, i solve my problem . foreach ($params->new_occ as $room => $occupants) { $myroom = $room + 1; $adults = (isset($occupants["adults"]) ? $occupants["adults"] : 0); $children = (isset($occupants["children"]) ? sizeof($occupants["children"]) : 0); $new_room_occ .= ' <tr> <td>'.$myroom.'</td> <td class="tc">'.$adults.'</td> <td>'.$children.'</td> </tr> '; } Thanks. Quote Link to comment 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.