php循环结果

时间:2011-12-26 06:50:54

标签: php loops for-loop

for($x=1;$x<=$total_room;$x++)
    {
     if($x%10==0){echo  '</br>';}
        foreach($room_status as $a=>$v)
        {
            $r = $v->room_no;
            if($x == $r)
            {
echo '<img src="'.SITE_NAME.IMAGE_DIR.'hotel_img/book.png"'.'height="25" width="25" >';
                $x++;
            }

        }
        if($x<=$total_room)
        {
        echo '|'.$x.'|' ;
        }
    }

在上面的循环中,$ tootal_room是数字 并且$ room_status是和这个数组的对象var_dump数组给我的结果如下

 0 => 
    object(stdClass)[8]
      public 'hotel_id' => string '1' (length=1)
      public 'room_no' => string '1' (length=1)
      public 'start_date' => string '2011-12-14' (length=10)
      public 'end_date' => string '2011-12-31' (length=10)
      public 'room_type' => string 'normal' (length=6)
  1 => 
    object(stdClass)[6]
      public 'hotel_id' => string '1' (length=1)
      public 'room_no' => string '5' (length=1)
      public 'start_date' => string '2011-12-14' (length=10)
      public 'end_date' => string '2011-12-31' (length=10)
      public 'room_type' => string 'normal' (length=6)
  2 => 
    object(stdClass)[9]
      public 'hotel_id' => string '1' (length=1)
      public 'room_no' => string '11' (length=2)
      public 'start_date' => string '2011-12-14' (length=10)
      public 'end_date' => string '2011-12-31' (length=10)
      public 'room_type' => string 'normal' (length=6)
  3 => 
    object(stdClass)[10]
      public 'hotel_id' => string '1' (length=1)
      public 'room_no' => string '8' (length=1)
      public 'start_date' => string '2011-12-14' (length=10)
      public 'end_date' => string '2011-12-31' (length=10)
      public 'room_type' => string 'normal' (length=6)
  4 => 
    object(stdClass)[11]
      public 'hotel_id' => string '1' (length=1)
      public 'room_no' => string '9' (length=1)
      public 'start_date' => string '2011-12-14' (length=10)
      public 'end_date' => string '2011-12-31' (length=10)
      public 'room_type' => string 'normal' (length=6)
  5 => 
    object(stdClass)[12]
      public 'hotel_id' => string '1' (length=1)
      public 'room_no' => string '4' (length=1)
      public 'start_date' => string '2011-12-14' (length=10)
      public 'end_date' => string '2011-12-31' (length=10)
      public 'room_type' => string 'normal' (length=6)
  6 => 
    object(stdClass)[13]
      public 'hotel_id' => string '1' (length=1)
      public 'room_no' => string '2' (length=1)
      public 'start_date' => string '2011-12-14' (length=10)
      public 'end_date' => string '2011-12-31' (length=10)
      public 'room_type' => string 'normal' (length=6)
  7 => 
    object(stdClass)[14]
      public 'hotel_id' => string '1' (length=1)
      public 'room_no' => string '7' (length=1)
      public 'start_date' => string '2011-12-14' (length=10)
      public 'end_date' => string '2011-12-31' (length=10)
      public 'room_type' => string 'normal' (length=6)

这个for循环没有给出room_no 5,8等预期的结果,这可能是错误

2 个答案:

答案 0 :(得分:0)

你正在使用X作为你的循环计数器,并在循环中使用X.对于每个匹配的房间,您可以通过在测试中递增x来跳过至少一条记录:

if ($x == $r) {
   ...
   $x++;
}

所以,如果$ x匹配(比如说)3,你将它改为4.然后循环再次滚动,PHP将x增加到5,你现在已经跳过测试#4。

答案 1 :(得分:0)

usort($room_status,function ($a,$b){
    if( $a->room_no==$b->room_no ){
        return 0;
    }elseif( $a->room_no<$b->room_no ){
        return -1;
    }else{
        return 1;
    }
});

for( $i=0,$c=count($room_status);$i<$c;$i++ ){
    echo '<img src="'.SITE_NAME.IMAGE_DIR.'hotel_img/book.png"'.'height="25" width="25" >|'.$room_status[$i]->room_no.'|';
}