开发者

3 time loop in present like html table?

开发者 https://www.devze.com 2023-02-23 01:24 出处:网络
I have an array: $A= array( \'A\'=> array(\'a1\'=>array(1,2,3), \'a2\'=>array(1,2,3), \'a3\'=>array(1,2,3)),

I have an array:

$A  = array(
         'A'=>
           array('a1'=>array(1,2,3),
                 'a2'=>array(1,2,3),
                 'a3'=>array(1,2,3)),
         'B' =>
            array('b1'=>array(1,2,3),
                  'b2'=>array(1,2,3),
                  'b3'=>array(1,2,3)),
         'C');

How Can I use loop to present data (html table ):

-----------------------------
|    | time1|  time1 | time2 |
----------------------------
A    | 3    |   6   |   8
-----------------------------
 a1  | 1    |   2   |  2
-----------------------------
 a2  | 1    |   2   |  3
-----------------------------
 a3  | 1    |   2   | 3
----------------------------
B
 b1
 b2
 b3
C

I am trying( get the total && display C instead 0):

echo '<table border="1">';
  echo "&l开发者_开发问答t;tr><td></td><td>time1</td><td>time2</td><td>time3</td></tr>";
 foreach ($A as $key=>$main){
    echo '<tr>';
      echo "<td>$key</td>";
      echo "<td>???</td>";
      echo "<td>???</td>";
      echo "<td>???</td>";
    echo '</tr>';

  foreach ($main as $k=>$sub) {
    echo '<tr>';
        echo "<td>$k</td>";

            foreach ($sub as $kk =>$val) {
                echo "<td>$val</td>";
            }
     echo '</tr>';    
      }


  }
  echo '</table>';


<?php

$A  = array(
         'A'=>
           array('a1'=>array(1,2,2),
                 'a2'=>array(1,2,3),
                 'a3'=>array(1,2,3)),
         'B' =>
            array('b1'=>array(1,2,3),
                  'b2'=>array(1,2,3),
                  'b3'=>array(1,2,3)),
         'C'=> null);   // in your case it won't be key it will be value

echo '<table border="1">';
echo "<tr><td></td><td>time1</td><td>time2</td><td>time3</td></tr>";

foreach($A as $key => $value)
{
   echo "<tr><td>".$key."</td>";
   if(!is_array($value))
     continue;

   echo "<td>".implode("</td><td>", findSum($value))."</td></tr>";
   foreach($value as $cKey => $cValue){
      echo "<tr><td>".$cKey."</td>"; 
      echo "<td>".implode("</td><td>", $cValue)."</td>";
   }
   echo "\n";
}

function findSum($value)
{
   if(!is_array($value))
      return array(0);

   $result = array();
   foreach($value as $childValue)
   {
      foreach($childValue as $cKey => $cValue)
          $result[$cKey] = isset($result[$cKey]) ? $result[$cKey] + $cValue : $cValue;
   }
   return $result;   
}
0

精彩评论

暂无评论...
验证码 换一张
取 消