Jump to content

rbaez

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.