2016-12-28 149 views
-1

如何用三個標題這是將它變成表格的形式:公司名稱(ABC和XYZ),科(古晉和詩巫兩家公司分別),並根據兩家公司的工作人員的名字(到陣列)。我是初學者。請幫助,因爲我卡住了。我的代碼有問題。PHP foreach循環與數組表

<html> 
<style> 
</style> 

<body> 
<?php 

$v_company= array(
    'ABC'=>array('Kuching'=>array('Michael', 'Jenny'), 
       'Sibu'=>array('Sally', 'Muhammad', 'Mutu') 
       ), 
    'XYZ'=>array('Kuching'=>array('Lucy', 'Abdullah'), 
       'Sibu'=>array('John', 'Alicia') 
    ) 
); 




?> 

<table> 

<tr><th>Company's Name</th> 
    <th>Branch</th> 
    <th>Staff's Name</th> 
</tr> 

<?php 

foreach($v_company as $v_company_name=>$v_company_info){ 
    echo "<tr>"; 
    echo "<td>"; 
    echo "$v_company_name <br>"; 
    echo "</td>"; 
    echo "</tr>"; 


    foreach($v_company_info as $v_branch=>$v_staffs){ 

       echo "<td>"; 
       echo "$v_branch <br/>"; 
       echo "</td>"; 


     foreach($v_staffs as $v_staff){ 
       echo "<td>"; 
       echo "$v_staff <br/>"; 
       echo "</td>"; 
      echo "Company: ". $v_company_name. ",Branch: ". $v_branch. ",Staff: " .$v_staff . "<br>"; 
     } 


    } 

} 

?> 
</table> 
</body> 
</html> 
+0

試試這個替代你更深入的foreach'的foreach($ v_staffs爲$ v_staff){回聲「​​」。爆( 「
」,$ v_staff)。 「」; }' – Bobot

回答

0

您可以使用此代碼

<html> 
    <style> 
    </style> 
    <body> 
     <?php 

$v_company= array(
    'ABC'=> 
     array('Kuching'=>array('Michael', 'Jenny'), 
      'Sibu'=>array('Sally', 'Muhammad', 'Mutu') 
       ), 
    'XYZ'=>array('Kuching'=>array('Lucy', 'Abdullah'), 
       'Sibu'=>array('John', 'Alicia') 
    ) 
); 





?> 
<table> 

<tr><th>Company's Name</th> 
    <th>Branch</th> 
    <th>Staff's Name</th> 
</tr> 
<?php 
$company = ''; 
$branch = ''; 
$staff = ''; 
foreach($v_company as $v_company_name=>$v_company_info){ 
    foreach($v_company_info as $v_branch=>$v_staffs){ 
     foreach($v_staffs as $v_staff){ 
       echo "<tr>"; 
        echo "<td>"; 
         if($company == '' || $company != $v_company_name){ 
          $company = $v_company_name; 
          echo "$v_company_name <br>"; 
         } 

        echo "</td>"; 
        echo "<td>"; 
         if($branch != $v_branch){ 
          $branch = $v_branch; 
          echo "$v_branch <br>"; 
         } 

        echo "</td>"; 
        echo "<td>"; 
         echo "$v_staff <br/>"; 
        echo "</td>"; 
       echo "</tr>"; 
     } 
    }  
} 
?> 
</table> 
</body> 
</html> 

您可以複製,如果你想粘貼。快樂編碼

+0

非常感謝,我會試着去了解它是如何工作的:) – user7349376

+0

當然。你是最受歡迎的! – rahulsm

+0

if($ branch!= $ v_branch){ – user7349376

0

我更新了你的foreach循環回聲值的試試這個:

<?php 

foreach($v_company as $v_company_name=>$v_company_info){ 

     $rowcount = 0;  

    foreach($v_company_info as $v_branch=>$v_staffs){ 

     echo "<tr>"; 
      echo "<td>"; 
       echo ($rowcount == 0)? $v_company_name : ""; 
      echo "</td>"; 

      echo "<td>"; 
      echo "$v_branch <br/>"; 
      echo "</td>"; 

      echo "<td>"; 


     foreach($v_staffs as $v_staff){ 
       echo "$v_staff <br/>"; 
     } 
      echo "</td>"; 
     echo "</tr>"; 
     $rowcount +=1; 
    } 

} 

?> 

這是你想要的輸出?

enter image description here

+0

什麼是你想要的輸出,給我以表格形式 –

+0

我已經更新了輸出圖像,這是你想要的輸出?告訴我。 –

+0

我把在下面的一個答案,對不起,如果它的麻煩 – user7349376

0

你可以用它來獲取所有的員工在不同的行

<html> 
<style> 
</style> 
<body> 
<?php 

$v_company= array(
    'ABC'=>array('Kuching'=>array('Michael', 'Jenny'), 
       'Sibu'=>array('Sally', 'Muhammad', 'Mutu') 
       ), 
    'XYZ'=>array('Kuching'=>array('Lucy', 'Abdullah'), 
       'Sibu'=>array('John', 'Alicia') 
    ) 
); 
?> 

<table border="1" width="100%"> 
<tr><th>Company's Name</th> 
    <th>Branch</th> 
    <th>Staff's Name</th> 
</tr> 
<?php 
foreach($v_company as $v_company_name=>$v_company_info){ 
    foreach($v_company_info as $v_branch=>$v_staffs){ 
     foreach($v_staffs as $v_staff){ 
      echo "<tr>"; 
    echo "<td>"; 
    echo "$v_company_name"; 

       echo "<td>"; 
       echo "$v_branch <br/>"; 
       echo "</td>"; 
       echo "<td>"; 
       echo "$v_staff <br/>"; 
       echo "</td>"; 
      echo "Company: ". $v_company_name. ",Branch: ". $v_branch. ",Staff: " .$v_staff . "<br>"; 
      echo "</td>"; 
    echo "</tr>"; 
     } 
    } 
} 

?> 
</table> 
</body> 
</html>