如何给阵列加分

时间:2010-10-06 21:55:32

标签: php

我有一个这样的数组(下面是var_dump)

array
  0 => 
    object(stdClass)[11]
      public 'VN' => string '571.5' (length=5)
      public 'color' => string 'GREEN' (length=5)
      public 'name' => string 'CIRRHOSIS' (length=9)
      public 'icd9ID' => string '5765' (length=4)
      public 'ID' => string '46741' (length=5)
  1 => 
    object(stdClass)[12]
      public 'VN' => string '571.5' (length=5)
      public 'color' => string 'GREEN' (length=5)
      public 'name' => string 'CIRRHOSIS' (length=9)
      public 'icd9ID' => string '5765' (length=4)
      public 'sortOrder' => string '1' (length=1)
      public 'ID' => string '46742' (length=5)
  2 => 
    object(stdClass)[15]
      public 'VN' => string 'V58.69' (length=6)
      public 'color' => string 'ORANGE' (length=6)
      public 'name' => string 'Long-term (current) use of other medications' (length=44)
      public 'icd9ID' => string '15116' (length=5)
      public 'ID' => string '46741' (length=5)
  3 => 
    object(stdClass)[14]
      public 'VN' => string '070.32' (length=6)
      public 'color' => string 'GREEN' (length=5)
      public 'name' => string 'HEPATITIS B CHRONIC' (length=19)
      public 'icd9ID' => string '14463' (length=5)
      public 'ID' => string '46742' (length=5)
  4 => 
    object(stdClass)[13]
      public 'VN' => string '070.32' (length=6)
      public 'color' => string 'GREEN' (length=5)
      public 'name' => string 'HEPATITIS B CHRONIC' (length=19)
      public 'icd9ID' => string '14463' (length=5)
      public 'ID' => string '46741' (length=5)

我想要两个HTML表格。一个将携带所有相同的ID,另一个将携带另一个ID

我该怎么做? 谢谢!

1 个答案:

答案 0 :(得分:1)

希望您可以使用它来输出您想到的HTML表格。

<?php
$destArray = Array() ;
foreach($srcArray as $key => $value) {
    if(is_object($value)) {
        $id = $value->ID ;
        if(!isset($destArray[$id])) {
            $destArray[$id] = Array() ;
        }
        $destArray[$id][$key] = $value ;
    }
}
foreach($destArray as $id => $obj) {
    // Start table for objects with ID $id
?>
    <table id="object_<? echo $id ; ?>">
<?php
    foreach($obj as $dkey => $dval) {
        // Write HTML table for object $dval. ($dkey holds original key if needed.)
?>
        <tr>
            <th>Name:</th>
            <td><? echo $dval->name ; ?></td>
        </tr>
        <tr>
            <th>Color:</th>
            <td><? echo $dval->color ; ?></td>
        </tr>
<?php
    }
?>
    </table>
<?php
}
?>