¿Cómo lo cambio en forma de tabla con tres encabezados que son: Nombre de la compañía (ABC y XYZ), Sucursal (Kuching y Sibu para ambas compañías respectivamente) y Nombre del personal (para ambas compañías de acuerdo con la matriz). Soy un principiante. Por favor ayuda ya que estoy atascado. Algo está mal con mi código.

<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>
-1
user7349376 28 dic. 2016 a las 14:35

3 respuestas

La mejor respuesta

Puedes usar este código

<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>

Puede copiar y pegar si lo desea. Codificación feliz

0
Rahul 28 dic. 2016 a las 12:39

Actualicé el valor de eco de tu bucle foreach. Prueba esto:

<?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;
    }  

}

?>

¿Es esta su salida deseada?

enter image description here

0
Amit Kumar Sahu 28 dic. 2016 a las 12:37

Puede usar esto para obtener a todas las empleadas en filas separadas.

<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>
0
sujivasagam 28 dic. 2016 a las 12:42