Foreach循环不起作用

时间:2015-12-27 22:47:59

标签: php loops foreach

任何人都可以请告诉我这段代码有什么问题?第一个if语句末尾的循环只返回一个结果而不是所有结果。我也附上了输出。非常感谢提前。

enter image description here

    <?php



if ( $instagramid ) {
 echo "


<br><br>
<span style=\"float:left; -webkit-border-radius: 3px;
border-radius: 3px;
font-size: 14px;
float: left;
line-height: 32px;
margin-right: 5px;
margin-top:12px;
padding: 0px 3px 0px 5px;
\">
<a target=\"_blank\" href=\"http://instagram.com/".$username."\"><img width=\"80\" border=\"1px\" src=\"".$profilepic."\"></a></span>
<br>
<div style=\"margin-left: 50px;\">
     <div style=\"position: relative;
   float: left;
   left: 0.00%;
   width: 75.00%;
   background-color: #f4f4f4\">
       <span style=\"
font-weight: bold;
font-style: normal;
font-size: 12px; letter-spacing: 0px; padding: 0px 0px 0px 0px; \"><a style=\"color: #3f729b; font-weight: bold; \" target=\"_blank\" href=\"http://instagram.com/".$username."\">".$username."</a> @ instagram</span>
    </div>
    <div style=\"position: relative;
   float: left;
   left: 0.00%;
   width: 82.00%;\">
        <div style=\"position: relative;
   float: left;
   left: 0.50%;
   width: 33.00%;\">
            <span style=\"
font-weight: lighter;
font-style: normal;
font-size: 11px; letter-spacing: 0px; padding: 0px 0px 0px 0px; \">Photos<br>
<span style=\"
font-weight: bold; \"><span style=\"
letter-spacing: 0px;\">".$sayi."</span></span></span>
        </div>
        <div style=\"position: relative;
   float: left;
   left: 1.50%;
   width: 33.00%;\">
            <span style=\"
font-weight: lighter;
font-style: normal;
font-size: 11px; letter-spacing: 0px; padding: 0px 0px 0px 0px; \">Followers<br>
 <span style=\"
font-weight: bold;\"><span style=\"
letter-spacing: 0px;\">".$takipci."</span></span></b></span>
        </div>
        <div style=\"position: relative;
   float: right;
   right: 0.50%;
   width: 31.00%;\">
          <span style=\"
font-weight: lighter;
font-style: normal;
font-size: 11px; letter-spacing: 0px; padding: 0px 0px 0px 0px; \">Following<br><span style=\"
font-weight: bold;\"><span style=\"
letter-spacing: 0px;\">".$takipediyor."</span></span></span> 
        </div> 
    </div>
   </div>
   <br><br>

    "
;




foreach ($result->data as $fotos)
$instagramlink=$fotos->link;
    $instagramthumb=$fotos->images->thumbnail->url;
    $instagramlikes=$fotos->likes->count;


 echo "
<!-- Renders images. @Options (thumbnail,low_resoulution, high_resolution) -->
        <span style=\"font-size: 12px; float:left; 
margin:5px 0 2px 10px;\">  
<a target=\"_blank\" class=\"group\" rel=\"group1\" href=\"".$instagramlink."\">
<img width=\"90\" src=\"".$instagramthumb."\"></a>
<span style=\"font-weight:bold; float:left;\"></span>
<span style=\"float:right;\"><span style=\"color:red; font-weight: bolder;\">❤ </span>".$instagramlikes."</span>
    </span>";

}


else {
echo "";
}
?>

对不起答案。这是var_dump的结果。

    array(6) {
  [0]=&gt;
  object(stdClass)#450 (15) {
    ["attribution"]=&gt;
    NULL
    ["tags"]=&gt;
    array(29) {
      [0]=&gt;
      string(11) "healthyfood"
      [1]=&gt;
      string(10) "gastronomi"
      [2]=&gt;
      string(8) "goodmood"
      [3]=&gt;
      string(6) "aegean"
      [4]=&gt;
      string(12) "egemutfağı"
      [5]=&gt;
      string(13) "istanbulnight"
      [6]=&gt;
      string(8) "goodfood"
      [7]=&gt;
      string(12) "istanbullife"
      [8]=&gt;
      string(9) "gastroart"
      [9]=&gt;
      string(5) "vegan"
      [10]=&gt;
      string(5) "gurme"
      [11]=&gt;
      string(18) "rokaperalezzetleri"
      [12]=&gt;
      string(9) "delicious"
      [13]=&gt;
      string(8) "rokapera"
      [14]=&gt;
      string(19) "sağlıklıyemekler"
      [15]=&gt;
      string(3) "eat"
      [16]=&gt;
      string(7) "cuisine"
      [17]=&gt;
      string(13) "rokaperabahce"
      [18]=&gt;
      string(8) "istanbul"
      [19]=&gt;
      string(15) "istanbuldayasam"
      [20]=&gt;
      string(8) "slowfood"
      [21]=&gt;
      string(8) "beyoğlu"
      [22]=&gt;
      string(9) "instafood"
      [23]=&gt;
      string(4) "food"
      [24]=&gt;
      string(6) "sunday"
      [25]=&gt;
      string(4) "meze"
      [26]=&gt;
      string(18) "istanbulrestaurant"
      [27]=&gt;
      string(5) "pazar"
      [28]=&gt;
      string(7) "fitfood"
    }
    ["type"]=&gt;
    string(5) "image"
    ["location"]=&gt;
    NULL
    ["comments"]=&gt;
    object(stdClass)#451 (2) {
      ["count"]=&gt;
      int(1)
      ["data"]=&gt;
      array(1) {
        [0]=&gt;
        object(stdClass)#452 (4) {
          ["created_time"]=&gt;
          string(10) "1451222250"
          ["text"]=&gt;
          string(37) "Bizim saksilarda da cok güzel oluyor"
          ["from"]=&gt;
          object(stdClass)#453 (4) {
            ["username"]=&gt;
            string(7) "rrahiya"
            ["profile_picture"]=&gt;
            string(107) "https://igcdn-photos-e-a.akamaihd.net/hphotos-ak-xpt1/t51.2885-19/11296890_1089406831069780_918421126_a.jpg"
            ["id"]=&gt;
            string(10) "1752184409"
            ["full_name"]=&gt;
            string(32) "Liberté, egalité, sensualité."
          }
          ["id"]=&gt;
          string(19) "1149257992582175831"
        }
      }
    }
    ["filter"]=&gt;
    string(6) "Normal"
    ["created_time"]=&gt;
    string(10) "1451222003"
    ["link"]=&gt;
    string(39) "https://www.instagram.com/p/_y-isiI5-I/"
    ["likes"]=&gt;
    object(stdClass)#454 (2) {
      ["count"]=&gt;
      int(147)
      ["data"]=&gt;
      array(4) {
        [0]=&gt;
        object(stdClass)#455 (4) {
          ["username"]=&gt;
          string(8) "cbnsibel"
          ["profile_picture"]=&gt;
          string(108) "https://igcdn-photos-f-a.akamaihd.net/hphotos-ak-xta1/t51.2885-19/11379317_1589253931356213_2037606977_a.jpg"
          ["id"]=&gt;
          string(10) "1986547402"
          ["full_name"]=&gt;
          string(5) "sibel"
        }
        [1]=&gt;
        object(stdClass)#456 (4) {
          ["username"]=&gt;
          string(18) "artisanotelalacati"
          ["profile_picture"]=&gt;
          string(99) "https://scontent.cdninstagram.com/hphotos-xfa1/t51.2885-19/11193131_105208333143926_294676378_a.jpg"
          ["id"]=&gt;
          string(10) "1992036455"
          ["full_name"]=&gt;
          string(22) "Artisan Otel Alaçatı"
        }
        [2]=&gt;
        object(stdClass)#457 (4) {
          ["username"]=&gt;
          string(10) "inci.yaras"
          ["profile_picture"]=&gt;
          string(108) "https://igcdn-photos-g-a.akamaihd.net/hphotos-ak-xft1/t51.2885-19/11191206_1844971219060510_1434357002_a.jpg"
          ["id"]=&gt;
          string(10) "2108058746"
          ["full_name"]=&gt;
          string(13) "İNCİ YARAŞ"
        }
        [3]=&gt;
        object(stdClass)#458 (4) {
          ["username"]=&gt;
          string(17) "zerrinellialtoglu"
          ["profile_picture"]=&gt;
          string(115) "https://igcdn-photos-c-a.akamaihd.net/hphotos-ak-xpa1/t51.2885-19/s150x150/12277636_518301508335162_120675683_a.jpg"
          ["id"]=&gt;
          string(10) "2090528591"
          ["full_name"]=&gt;
          string(18) "Zerrinellialtioglu"
        }
      }
    }
    ["images"]=&gt;
    object(stdClass)#459 (3) {
      ["low_resolution"]=&gt;
      object(stdClass)#460 (3) {
        ["url"]=&gt;
        string(111) "https://scontent.cdninstagram.com/hphotos-xtp1/t51.2885-15/s320x320/e35/12394199_518383638336848_87665054_n.jpg"
        ["width"]=&gt;
        int(320)
        ["height"]=&gt;
        int(320)
      }
      ["thumbnail"]=&gt;
      object(stdClass)#461 (3) {
        ["url"]=&gt;
        string(128) "https://scontent.cdninstagram.com/hphotos-xft1/t51.2885-15/s150x150/e35/c0.120.960.960/11261430_893913227383126_1405649502_n.jpg"
        ["width"]=&gt;
        int(150)
        ["height"]=&gt;
        int(150)
      }
      ["standard_resolution"]=&gt;
      object(stdClass)#462 (3) {
        ["url"]=&gt;
        string(118) "https://scontent.cdninstagram.com/hphotos-xtp1/t51.2885-15/s640x640/sh0.08/e35/12394199_518383638336848_87665054_n.jpg"
        ["width"]=&gt;
        int(640)
        ["height"]=&gt;
        int(640)
      }
    }
    ["users_in_photo"]=&gt;
    array(0) {
    }
    ["caption"]=&gt;
    object(stdClass)#463 (4) {
      ["created_time"]=&gt;
      string(10) "1451222003"
      ["text"]=&gt;
      string(355) "Keyifli Pazarlar 
#rokapera #egemutfağı #sağlıklıyemekler #vegan #aegean #cuisine #fitfood #meze #istanbul 
#rokaperabahce #rokaperalezzetleri #istanbul #istanbullife #istanbulrestaurant #istanbulnight #istanbuldayasam #beyoğlu #eat #delicious #gurme #gastronomi #gastroart #goodfood #goodmood #food  #healthyfood #instafood #slowfood #sunday #pazar"
      ["from"]=&gt;
      object(stdClass)#464 (4) {
        ["username"]=&gt;
        string(13) "rokaperabahce"
        ["profile_picture"]=&gt;
        string(108) "https://scontent.cdninstagram.com/hphotos-xaf1/t51.2885-19/s150x150/11909310_888061464574620_968185939_a.jpg"
        ["id"]=&gt;
        string(10) "1550397278"
        ["full_name"]=&gt;
        string(9) "Roka Pera"
      }
      ["id"]=&gt;
      string(19) "1149255927407550385"
    }
    ["user_has_liked"]=&gt;
    bool(false)
    ["id"]=&gt;
    string(30) "1149255918096195464_1550397278"
    ["user"]=&gt;
    object(stdClass)#465 (4) {
      ["username"]=&gt;
      string(13) "rokaperabahce"
      ["profile_picture"]=&gt;
      string(108) "https://scontent.cdninstagram.com/hphotos-xaf1/t51.2885-19/s150x150/11909310_888061464574620_968185939_a.jpg"
      ["id"]=&gt;
      string(10) "1550397278"
      ["full_name"]=&gt;
      string(9) "Roka Pera"
    }
  }
  [1]=&gt;
  object(stdClass)#466 (15) {
    ["attribution"]=&gt;
    NULL
    ["tags"]=&gt;
    array(29) {
      [0]=&gt;
      string(11) "healthyfood"
      [1]=&gt;
      string(10) "gastronomi"
      [2]=&gt;
      string(8) "goodmood"
      [3]=&gt;
      string(6) "aegean"
      [4]=&gt;
      string(12) "egemutfağı"
      [5]=&gt;
      string(13) "istanbulnight"
      [6]=&gt;
      string(8) "goodfood"
      [7]=&gt;
      string(12) "istanbullife"
      [8]=&gt;
      string(9) "gastroart"
      [9]=&gt;
      string(5) "vegan"
      [10]=&gt;
      string(5) "gurme"
      [11]=&gt;
      string(18) "rokaperalezzetleri"
      [12]=&gt;
      string(9) "delicious"
      [13]=&gt;
      string(8) "rokapera"
      [14]=&gt;
      string(19) "sağlıklıyemekler"
      [15]=&gt;
      string(8) "saturday"
      [16]=&gt;
      string(7) "cuisine"
      [17]=&gt;
      string(13) "rokaperabahce"
      [18]=&gt;
      string(6) "flower"
      [19]=&gt;
      string(8) "istanbul"
      [20]=&gt;
      string(15) "istanbuldayasam"
      [21]=&gt;
      string(8) "slowfood"
      [22]=&gt;
      string(8) "beyoğlu"
      [23]=&gt;
      string(3) "eat"
      [24]=&gt;
      string(9) "instafood"
      [25]=&gt;
      string(4) "food"
      [26]=&gt;
      string(4) "meze"
      [27]=&gt;
      string(18) "istanbulrestaurant"
      [28]=&gt;
      string(7) "fitfood"
    }
    ["type"]=&gt;
    string(5) "image"
    ["location"]=&gt;
    object(stdClass)#467 (4) {
      ["latitude"]=&gt;
      float(41.029172249)
      ["name"]=&gt;
      string(18) "Roka Pera Istanbul"
      ["longitude"]=&gt;
      float(28.97304208)
      ["id"]=&gt;
      int(226441757)
    }
    ["comments"]=&gt;
    object(stdClass)#468 (2) {
      ["count"]=&gt;
      int(0)
      ["data"]=&gt;
      array(0) {
      }
    }
    ["filter"]=&gt;
    string(6) "Normal"
    ["created_time"]=&gt;
    string(10) "1451143170"
    ["link"]=&gt;
    string(39) "https://www.instagram.com/p/_woLm7o571/"
    ["likes"]=&gt;
    object(stdClass)#469 (2) {
      ["count"]=&gt;
      int(124)
      ["data"]=&gt;
      array(4) {
        [0]=&gt;
        object(stdClass)#470 (4) {
          ["username"]=&gt;
          string(10) "inci.yaras"
          ["profile_picture"]=&gt;
          string(108) "https://igcdn-photos-g-a.akamaihd.net/hphotos-ak-xft1/t51.2885-19/11191206_1844971219060510_1434357002_a.jpg"
          ["id"]=&gt;
          string(10) "2108058746"
          ["full_name"]=&gt;
          string(13) "İNCİ YARAŞ"
        }
        [1]=&gt;
        object(stdClass)#471 (4) {
          ["username"]=&gt;
          string(8) "nerminhm"
          ["profile_picture"]=&gt;
          string(99) "https://scontent.cdninstagram.com/hphotos-xfa1/t51.2885-19/11375775_368087600058975_581007122_a.jpg"
          ["id"]=&gt;
          string(10) "2125331353"
          ["full_name"]=&gt;
          string(22) "Nermin Hocaoğlu Mutlu"
        }
        [2]=&gt;
        object(stdClass)#472 (4) {
          ["username"]=&gt;
          string(12) "betulozkayaa"
          ["profile_picture"]=&gt;
          string(110) "https://scontent.cdninstagram.com/hphotos-xpt1/t51.2885-19/s150x150/12353384_1688926777992013_1604858269_a.jpg"
          ["id"]=&gt;
          string(10) "2188974742"
          ["full_name"]=&gt;
          string(14) "Betül Özkaya"
        }
        [3]=&gt;
        object(stdClass)#473 (4) {
          ["username"]=&gt;
          string(10) "comoburger"
          ["profile_picture"]=&gt;
          string(100) "https://scontent.cdninstagram.com/hphotos-xaf1/t51.2885-19/11378524_1409822219347310_349229698_a.jpg"
          ["id"]=&gt;
          string(10) "2130593263"
          ["full_name"]=&gt;
          string(17) "Hamburger Gourmet"
        }
      }
    }
    ["images"]=&gt;
    object(stdClass)#474 (3) {
      ["low_resolution"]=&gt;
      object(stdClass)#498 (3) {
        ["url"]=&gt;
        string(113) "https://scontent.cdninstagram.com/hphotos-xft1/t51.2885-15/s320x320/e35/12362432_600511983432932_1619827376_n.jpg"
        ["width"]=&gt;
        int(320)
        ["height"]=&gt;
        int(320)
      }
      ["thumbnail"]=&gt;
      object(stdClass)#497 (3) {
        ["url"]=&gt;
        string(131) "https://scontent.cdninstagram.com/hphotos-xtp1/t51.2885-15/s150x150/e35/c0.135.1080.1080/12345975_1506337882995996_2045435867_n.jpg"
        ["width"]=&gt;
        int(150)
        ["height"]=&gt;
        int(150)
      }
      ["standard_resolution"]=&gt;
      object(stdClass)#496 (3) {
        ["url"]=&gt;
        string(120) "https://scontent.cdninstagram.com/hphotos-xft1/t51.2885-15/s640x640/sh0.08/e35/12362432_600511983432932_1619827376_n.jpg"
        ["width"]=&gt;
        int(640)
        ["height"]=&gt;
        int(640)
      }
    }
    ["users_in_photo"]=&gt;
    array(0) {
    }
    ["caption"]=&gt;
    object(stdClass)#495 (4) {
      ["created_time"]=&gt;
      string(10) "1451143170"
      ["text"]=&gt;
      string(375) "Cumartesi Çiçekleri 

1 个答案:

答案 0 :(得分:0)

虽然我们发现您的foreach循环没有开放{,但您的if语句也没有结尾}。尝试将该代码更改为:

if ( $instagramid ) 
{
    echo "


<br><br>
<span style=\"float:left; -webkit-border-radius: 3px;
border-radius: 3px;
font-size: 14px;
float: left;
line-height: 32px;
margin-right: 5px;
margin-top:12px;
padding: 0px 3px 0px 5px;
\">
<a target=\"_blank\" href=\"http://instagram.com/".$username."\"><img width=\"80\" border=\"1px\" src=\"".$profilepic."\"></a></span>
<br>
<div style=\"margin-left: 50px;\">
     <div style=\"position: relative;
   float: left;
   left: 0.00%;
   width: 75.00%;
   background-color: #f4f4f4\">
       <span style=\"
font-weight: bold;
font-style: normal;
font-size: 12px; letter-spacing: 0px; padding: 0px 0px 0px 0px; \"><a style=\"color: #3f729b; font-weight: bold; \" target=\"_blank\" href=\"http://instagram.com/".$username."\">".$username."</a> @ instagram</span>
    </div>
    <div style=\"position: relative;
   float: left;
   left: 0.00%;
   width: 82.00%;\">
        <div style=\"position: relative;
   float: left;
   left: 0.50%;
   width: 33.00%;\">
            <span style=\"
font-weight: lighter;
font-style: normal;
font-size: 11px; letter-spacing: 0px; padding: 0px 0px 0px 0px; \">Photos<br>
<span style=\"
font-weight: bold; \"><span style=\"
letter-spacing: 0px;\">".$sayi."</span></span></span>
        </div>
        <div style=\"position: relative;
   float: left;
   left: 1.50%;
   width: 33.00%;\">
            <span style=\"
font-weight: lighter;
font-style: normal;
font-size: 11px; letter-spacing: 0px; padding: 0px 0px 0px 0px; \">Followers<br>
 <span style=\"
font-weight: bold;\"><span style=\"
letter-spacing: 0px;\">".$takipci."</span></span></b></span>
        </div>
        <div style=\"position: relative;
   float: right;
   right: 0.50%;
   width: 31.00%;\">
          <span style=\"
font-weight: lighter;
font-style: normal;
font-size: 11px; letter-spacing: 0px; padding: 0px 0px 0px 0px; \">Following<br><span style=\"
font-weight: bold;\"><span style=\"
letter-spacing: 0px;\">".$takipediyor."</span></span></span> 
        </div> 
    </div>
   </div>
   <br><br>

    "
;




    foreach ($result->data as $fotos)
    {
        $instagramlink=$fotos->link;
        $instagramthumb=$fotos->images->thumbnail->url;
        $instagramlikes=$fotos->likes->count;


         echo "
<!-- Renders images. @Options (thumbnail,low_resoulution, high_resolution) -->
        <span style=\"font-size: 12px; float:left; 
margin:5px 0 2px 10px;\">  
<a target=\"_blank\" class=\"group\" rel=\"group1\" href=\"".$instagramlink."\">
<img width=\"90\" src=\"".$instagramthumb."\"></a>
<span style=\"font-weight:bold; float:left;\"></span>
<span style=\"float:right;\"><span style=\"color:red; font-weight: bolder;\">❤ </span>".$instagramlikes."</span>
    </span>";

    }
}
else 
{
    echo "";
}

如果您在整个代码中使用干净且一致的格式,则很容易发现这些类型的错误!如果有问题,请告诉我。