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. Link to comment https://forums.phpfreaks.com/topic/273246-iterating-array/ 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"; } Link to comment https://forums.phpfreaks.com/topic/273246-iterating-array/#findComment-1406192 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. Link to comment https://forums.phpfreaks.com/topic/273246-iterating-array/#findComment-1406207 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. Link to comment https://forums.phpfreaks.com/topic/273246-iterating-array/#findComment-1406209 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. Link to comment https://forums.phpfreaks.com/topic/273246-iterating-array/#findComment-1406211 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. Link to comment https://forums.phpfreaks.com/topic/273246-iterating-array/#findComment-1406234 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.