Generate table from multi-array using php function


#1


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


#2

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?


#3

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

#4

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


#5

okay.thanx.its working :slight_smile:


closed #6

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