tags:

views:

27

answers:

1

I have an array like this (below is the 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)

I want to two HTML tables. One that will carry all the same ID and second one will carry the other ID

How can I do that Thanks!

+1  A: 

Hopefully you can use this to output the HTML table you had in mind.

<?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
}
?>
Gus
thanks Gus. can you give me an example of the usage of this part
foreach($destArray as $id => $obj) { // Start table for objects with ID $id foreach($obj as $dkey => $dval) { // Write HTML table for object $dval. ($dkey holds original key if needed.) } }
when I did this foreach($destArray as $id => $obj) { // Start table for objects with ID $id print $id; I got an error
here is the error Catchable fatal error: Object of class stdClass could not be converted to string
I have updated my answer with a basic tabluation of the object. I'm not sure what caused that error but it sounds like you tried to output the object held in $dval. Did you add a: `print $dval ;` or something similar?
Gus