PHP Foreach循环无法与变量

问题描述:

对不起,如果这是重复的帖子,但不知何故,我不能让我的循环与变量工作。如果我回声这里面foreach我得到我的数据,但如果我使用变量我只获得我的数组中的第一项数据。PHP Foreach循环无法与变量

我的代码`

 $user - My arrray 
     $count = $user['count']; 
     $echo = ""; 
     foreach($user as $val => $key) 
     { 
      if($val == 'true') 
      { 
       for($x = 0; $x < $count; $x++) 
       { 
        $name = $user[$val][$x]['name']; 
        $id = $user[$val][$x]['id']; 
        $staatus = $user[$val][$x]['status']; 
        $feed = $user[$val][$x]['feed']; 
        /* Now when am using only echo, instead of $echo it will work flawlessly, 
        but i want to echo it using an variable called $echo. 
        I call the variable on my html page where the forms are, 
        but i'm only getting data for the first element. 
        Short, i want to display my data under the form.*/ 
        $echo = " 
        <table class='table'> 
         <tr> 
          <th>NIMI</th> 
          <th>ID</th> 
          <th>STAATUS</th> 
          <th>FEED</th> 
         </tr> 
         <tr> 
          <td>$name</td> 
          <td>$id</td> 
          <td>$staatus</td> 
          <td>$feed</td> 
         </tr> 
        </table> 
         "; 
       } 
      } 

     } 
     <?php echo $echo; ?> for calling. 

我在这件事情很是初学者所以也许有人可以帮助。我也四处搜寻,尝试了不同的东西,但它不起作用。我也尝试在没有for循环的情况下显示数据,但它也不起作用。

此外,如果有任何改进我可以用我的代码做请告诉我。就像把它缩短等,我在这里,以提高岂非:d

你缺少.$echo .= "

$echo .= " 
<table class='table'> 
    <tr> 
     <th>NIMI</th> 
     <th>ID</th> 
     <th>STAATUS</th> 
     <th>FEED</th> 
    </tr> 
    <tr> 
     <td>$name</td> 
     <td>$id</td> 
     <td>$staatus</td> 
     <td>$feed</td> 
    </tr> 
</table> 
    "; 
+0

它的工作现在,谢谢。我真的不知道我需要。= on echo。我没有看到,当我谷歌。 – drpzz

+0

'。='只是意味着“将此添加到已经存在于此变量中”。所以基本上它扩展了该变量中的字符串。 –