Generate table from multi-array using php function


.i have multi array. now i want to show data as like this image.i am trying but not see this .how to make this type table using php function.

It would be a good idea to show the code you used to generate the array as it might have an effect on any answers. I assume it comes from a database?

1 Like
<?php  
$std=[
      ["id"=>1,"name"=>"maksud","mobile"=>"0187","address"=>"chandpur"],
      ["id"=>2,"name"=>"tonmoy","mobile"=>"0177","address"=>"dhaka"],
      ["id"=>3,"name"=>"abir","mobile"=>"0155","address"=>"khulna"]];
class tb{
	private $table;
	function get_table($data){
		$this->table="<table border=\"1\">";

		foreach($data[0] as $key=> $val){
					$this->table.="<tr>";
 			$this->table.="<td> $key </td>";
 	foreach($data as $row){
 		foreach($row as $value){
 			$this->table.="<td> $value </td>";
 		}
 	}
 }
 			$this->table.="</tr>";
	$this->table.="</table>";
		return $this->table;
}
}
$obj=new tb();
echo $obj->get_table($std);
?>
<?php

$data=[
      ["id"=>1,"name"=>"maksud","mobile"=>"0187","address"=>"chandpur"],
      ["id"=>2,"name"=>"tonmoy","mobile"=>"0177","address"=>"dhaka"],
      ["id"=>3,"name"=>"abir","mobile"=>"0155","address"=>"khulna"]];
    
echo '<table><tbody>'; 
foreach(['id','name','mobile'] as $attribute) {
    echo '<tr><td>'.$attribute.'</td>';
    foreach($data as $row) {
        echo '<td>'.$row[$attribute].'</td>';
    }
    echo '</tr>';
}
echo '</tbody></table>';

There is also a more clever way to do it with flex but that is the simplest.

1 Like

okay.thanx.its working :slight_smile:

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.