无法使用PHP根据json数组值创建动态表

无法使用PHP根据json数组值创建动态表,php,html-table,Php,Html Table,我试图使用PHP按照json数组将动态值附加到表中,但无法将其转换为正确的格式。我在下面解释我的代码 静态表: 区 中心 研究所 西区 BBSR 学院1 机构2 机构3 科尔达 机构4 机构5 东区 卡塔克 学院6 学院7 科尔达 学院8 北区 普里 学院9 龟头疮 机构10 这是我的表,它有静态数据,这里我需要使它成为动态的。我在下面解释php代码 table.php: 这里我的要求是,我需要将这些数组值附加到该表中,结果应该与正确的原始表格式一起提供。 <?php $resul

我试图使用PHP按照json数组将动态值附加到表中,但无法将其转换为正确的格式。我在下面解释我的代码

静态表:


区
中心
研究所
西区
BBSR
学院1
机构2
机构3
科尔达
机构4
机构5
东区
卡塔克
学院6
学院7
科尔达
学院8
北区
普里
学院9
龟头疮
机构10
这是我的表,它有静态数据,这里我需要使它成为动态的。我在下面解释php代码

table.php:


这里我的要求是,我需要将这些数组值附加到该表中,结果应该与正确的原始表格式一起提供。


<?php
$resultArr = 
[
  [
    "title"=>"West Zone",
    "children"=>
    [
      [
        "title"=>"BBSR",
        "children"=>      
        [
          ["title"=>"Institute1"],
          ["title"=>"Institute2"],
          ["title"=>"Institute3"]
        ]
      ],
      [
        "title"=>"Khordha",
        "children"=>
        [
          ["title"=>"Institute4"],
          ["title"=>"Institute5"]
        ]
      ]
    ]
  ],
  [
    "title"=>"East zone",
    "children"=>
    [
      [
        "title"=>"Cuttack",
        "children"=>
        [
          ["title"=>"Institute6"],
          ["title"=>"Institute7"]
        ]
      ],
      [
        "title"=>"Khordha",
        "children"=>
        [
          ["title"=>"Institute8"]
        ]
      ]
    ]
  ],
  [
    "title"=>"North Zone",
    "children"=>
    [
      [
        "title"=>"Puri",
        "children"=>
        [
          ["title"=>"Institute9"]
        ]
      ],
      [
        "title"=>"Balasore",
        "children"=>
        [
          ["title"=>"Institute10"]
        ]
      ]
    ]
  ]
];

$html = '<table width="60%" cellspacing="0" border="1">
  <thead>
    <tr>
      <th>Zone</th>
      <th>Centre</th>
      <th>Institute</th>
    </tr>
  </thead>
  <tbody>';
$table = buildTable($resultArr,0);
$html.= $table[0];
$html.= '</tbody></table>';
echo $html;

function buildTable($tbl,$level)
{
  $cnt = 0;
  $res = '';
  foreach ($tbl as $idx => &$row) 
  {
    if($level==0 OR $idx>0) $res.= '<tr>';
    if(isset($row['children']) AND is_array($row['children'])) 
    {
      $child = buildTable($row['children'],$level+1);
      $res.= '<td rowspan="'.$child[1].'">'.$row['title'].'</td>'.$child[0];
      $cnt+= $child[1];
    }
    else
    {
      $res.= '<td>'.$row['title'].'</td></tr>';
      $cnt++;
    }
  }
  return Array($res,$cnt);
}

?>

这里有什么不起作用呢?您确实意识到您用每一行新的
$subcen=…
覆盖了
$subcen
。@kerbholz:如果您回显
$resultar
,您可以看到数组格式是正确的。在这里,我只是混淆了如何使用动态数据制作正确的表格格式。啊,我的错,我错过了
“subdata”=>$subcen
<?php
$subcen=array(array("Institute"=>"Institute1"),array("Institute"=>"Institute2"),array("Institute"=>"Institute3"));
$cenArr[]=array("center"=>"BBSR","subdata"=>$subcen);
$subcen=array(array("Institute"=>"Institute4"),array("Institute"=>"Institute5"));
$cenArr[]=array("center"=>"Khordha","subdata"=>$subcen);
$resultArr[]=array("zone"=>"West Zone","centerData"=>$cenArr);
$cenArr=array();
$subcen=array(array("Institute"=>"Institute6"),array("Institute"=>"Institute7"));
$cenArr[]=array("center"=>"Cuttack","subdata"=>$subcen);
$subcen=array(array("Institute"=>"Institute8"));
$cenArr[]=array("center"=>"Khordha","subdata"=>$subcen);
$resultArr[]=array("zone"=>"East zone","centerData"=>$cenArr);
$cenArr=array();
$subcen=array(array("Institute"=>"Institute9"));
$cenArr[]=array("center"=>"Puri","subdata"=>$subcen);
$subcen=array(array("Institute"=>"Institute10"));
$cenArr[]=array("center"=>"Balasore","subdata"=>$subcen);
$resultArr[]=array("zone"=>"North Zone","centerData"=>$cenArr);
//echo json_encode($resultArr);
$html='';
$html.='<table width="60%" cellspacing="0" border="1"><thead><tr><th>Zone</th><th>Centre</th><th>Institute</th></tr></thead><tbody>';
foreach ($resultArr as $value) {
    $zonecnt=0;
    foreach ($value['centerData'] as $v) {
        $centercnt=0;
        foreach ($v['subdata'] as $val) {
            $centercnt=$centercnt+count($val);
            $zonecnt=$zonecnt+count($val);
        }
        //echo $centercnt.'</br>';
    }
    //echo $zonecnt.'</br>';
}

?>
<?php
$resultArr = 
[
  [
    "title"=>"West Zone",
    "children"=>
    [
      [
        "title"=>"BBSR",
        "children"=>      
        [
          ["title"=>"Institute1"],
          ["title"=>"Institute2"],
          ["title"=>"Institute3"]
        ]
      ],
      [
        "title"=>"Khordha",
        "children"=>
        [
          ["title"=>"Institute4"],
          ["title"=>"Institute5"]
        ]
      ]
    ]
  ],
  [
    "title"=>"East zone",
    "children"=>
    [
      [
        "title"=>"Cuttack",
        "children"=>
        [
          ["title"=>"Institute6"],
          ["title"=>"Institute7"]
        ]
      ],
      [
        "title"=>"Khordha",
        "children"=>
        [
          ["title"=>"Institute8"]
        ]
      ]
    ]
  ],
  [
    "title"=>"North Zone",
    "children"=>
    [
      [
        "title"=>"Puri",
        "children"=>
        [
          ["title"=>"Institute9"]
        ]
      ],
      [
        "title"=>"Balasore",
        "children"=>
        [
          ["title"=>"Institute10"]
        ]
      ]
    ]
  ]
];

$html = '<table width="60%" cellspacing="0" border="1">
  <thead>
    <tr>
      <th>Zone</th>
      <th>Centre</th>
      <th>Institute</th>
    </tr>
  </thead>
  <tbody>';
$table = buildTable($resultArr,0);
$html.= $table[0];
$html.= '</tbody></table>';
echo $html;

function buildTable($tbl,$level)
{
  $cnt = 0;
  $res = '';
  foreach ($tbl as $idx => &$row) 
  {
    if($level==0 OR $idx>0) $res.= '<tr>';
    if(isset($row['children']) AND is_array($row['children'])) 
    {
      $child = buildTable($row['children'],$level+1);
      $res.= '<td rowspan="'.$child[1].'">'.$row['title'].'</td>'.$child[0];
      $cnt+= $child[1];
    }
    else
    {
      $res.= '<td>'.$row['title'].'</td></tr>';
      $cnt++;
    }
  }
  return Array($res,$cnt);
}

?>