如何在for循环之外获取值? [关闭]

I am sending a hotel list request. My URL is:

&numberOfRooms=2&room-0-adult-total=1&room-0-child-total=1&room-1-adult-total=1&room-1-child-total=0&room-0-child-0-age=2&button=search

I need a result for this method:

&room1=2,3,5    (2 Adults, 2 Children Ages 3 & 5)
&room2=2,10  (2 Adults, 1 Children Ages 10)

How can I get this output?

Here is my code:

for ($i=0;$i<$arraoy[numberOfRooms];$i++)
{
    echo"room_adut$i=";
    echo $adult[] = $arraoy['room-'.$i.'-adult-total'];
    $child =$arraoy['room-'.$i.'-child-total'];
    echo",";

    for($j=0;$j<$child =$arraoy['room-'.$i.'-child-total'];$j++)
    {
        echo $age[]= $arraoy['room-'.$i.'-child-'.$j.'-age'];
        echo",";
    }

    echo"<br>";
}

I can print the inner for-loop and the result is correct. But how can I get the value outside of the for-loop?

You can use arrays to collect all the data and then comma separate them at the end of each loop:

for ($i = 0; $i < $arraoy['numberOfRooms']; $i++) {
    $prefix = "room-$i";

    $values = array($arraoy["$prefix-adult-total"]);

    for($j = 0; $j < $arraoy["$prefix-child-total"]; $j++) {
        $values[] = $arraoy["$prefix-child-$j-age"];
    }
    echo 'room' . ($i + 1) . '=' . join(',', $values), '<br>';
}

If I understand you correctly, you want to get the $age variable to be accesible outside the loop. If so just define it before the loop. In general define variables outside loops to contain values after the loop is done.

$age = array();
for() {
  ...
}
// Handle $age values

Try using following code

$arrvar="";
for($i=0;$i<$arraoy[numberOfRooms];$i++){
  $arrvar.="room_adut$i=";

  $arrvar.= $arraoy['room-'.$i.'-adult-total'];
  $child =$arraoy['room-'.$i.'-child-total'];
  $arrvar.=",";
  for($j=0;$j<$child =$arraoy['room-'.$i.'-child-total'];$j++)
  {
    $arrvar.= $arraoy['room-'.$i.'-child-'.$j.'-age'];
    $arrvar.=",";
  }
  $arrvar.="<br>";
} 

echo $arrvar;