Teil von  SELFPHP   Teil von  Code Snippets / PHP / Arrays
Letztes Update: 04.09.2007 14:57:59

Navigation

Seite Startseite
Seite Aktuelles
Seite Seite News*
Seite Seite Pressemitteilungen*
Seite SELFPHP
Seite Seite Über SELFPHP
Seite Seite Werbung
Seite Seite Kontakt
Seite Seite Unsere Banner
Seite Seite Impressum
Seite Qozido
Seite Seite Diving Edition
Seite Seite Snorkeling Edition
Seite SELFPHP Dokumentation
Seite Seite PHP-Skripte
Seite Seite PHP Befehlsreferenz
Seite Seite PHP 5 Praxisbuch
Seite Seite PHP Code Snippets
Seite Seite SELFPHP in Buchform
Seite Anbieterverzeichnis
Seite Seite Globale Branchen
Seite Seite Webhosting/Server
Seite Services
Seite Seite Forum*
Seite Seite RSS-Feeds*
Seite Seite Downloads*
Seite Seite Gratis-Video-Lektionen*
Seite Seite SELFPHP auf Heft-CD*
Seite Seite Newsletter*
Seite Internet Security
 sponsored by  
Seite Stellenangebote*
Sedo - Bei uns wird PHP großgeschrieben
Seite Premium-Partner   
Webhosting/Serverlösungen
Seite Produktempfehlung   
Seite Seite SteadyBackup

 
* Link führt ins Internet


Anbieterverzeichnis
Informieren Sie sich über die Unternehmen in unserem Anbieterverzeichnis!  
 


SELFPHP Forum
Fragen rund um die Themen PHP? In über 100.000 Beiträgen finden Sie sicher die passende Antwort!*  


Newsletter
Abonnieren Sie hier den kostenlosen SELFPHP Newsletter!*

Vorname: 
Name:
E-Mail:
 




 

Ein Array als graphischen Tabellendump darstellen





Beispielaufgabe

Ein Array - auch multidimensional - soll als graphischer Tabellendump dargestellt werden.



Beschreibung

Die folgende Funktion "DumpArrayToTable" legt zu Beginn einen Random-Farbwert für den Tabellenrand fest (auch bei rekursiven Funktionsaufrufen). Nach dem geprüft wurde, ob der aktuell übergebene Wert ein Array ist - wenn nicht, dann wird der Wert in ein Array umgewandelt - durchläuft die Funktion alle Array-Elemente und Array-Werte. Ist eine weitere Dimension vorhanden, ruft sich die Funktion erneut selbst auf (Rekursion), ist keine weitere Dimension vorhanden, dann wird der aktuelle Wert ausgegeben. Als kleines Extra, wird beim Zeigen auf eine Tabellenzelle, der Type des Wertes als Tooltip angezeigt, auch bei den Zellen der Spaltenüberschriften.



<?PHP
  
function DumpArrayToTable $arDest )
  {
    
// Random-Farbe fuer den Rand der Tabellen.
    
$iBorderColor str_pad mt_rand 0999999 ), 6,
                              
'0'STR_PAD_LEFT );

    print ( 
'<table cellpadding="0" cellspacing="5" ' );
    print ( 
'style="border: 4px solid #' $iBorderColor '; ');
    print ( 
'font-family: Courier New; font-size: .9em;">' );

    
// Wenn kein Array, dann Input in ein Array umwandeln.
    
if ( ! is_array $arDest[0] ) )
    {
      
$arDest = array ( $arDest );
    }

    print ( 
'<tr>' );

    
// Schluessel des Arrays als Tabellenspaltenueberschrift.
    
foreach ( array_keys $arDest[0] ) as $strTmpT )
    {
      
$strType 'Type: ' gettype $strTmpT );

      print ( 
'<td title="' $strType '" style="text-align: center;' );
      print ( 
' font-weight: bold; border: 2px solid #000; padding:' );
      print ( 
' 3px; background-color: #ccc;">' $strTmpT '</td>' );
    }

    print ( 
'</tr>' );

    
// Array durchlaufen.
    
foreach ( $arDest as $arTmpElm )
    {
      print ( 
'<tr>' );

      foreach ( 
$arTmpElm as $strTmpVal )
      {
        
$strType 'Type: ' gettype $strTmpVal );

        print ( 
'<td title="' $strType '" style="text-align: ' );
        print ( 
'center; border: 2px solid #000; padding: 3px;">' );

        if ( 
is_array $strTmpVal ) && ! empty ( $strTmpVal ) )
        {
          
// Wenn ein Array in einem Array, dann
          // rekursiv DumpArrayToTable aufrufen.
          
DumpArrayToTable $strTmpVal );
        }
        else
        {
          if ( ! empty ( 
$strTmpVal ) )
          {
            print ( 
$strTmpVal );
          }
          else
          {
            
// Wenn aktuelles Array-Element leer ist,
            // dann Tabellenzelle mit "empty" markieren.
            
print ( 'empty' );
          }
        }

        print ( 
'</td>' );
      }

      print ( 
'</tr>' );
    }

    print ( 
'</table>' );
  }
?>



Anwendungsbeispiel

<?PHP
  
// Test-Array
  
$arT = array (
                 array (
                         
'A' => 1,
                         
'B' => 2,
                         
'C' => NULL,
                         
'D' => 4,
                         
'E' => 5
                       
),
                 array (
                         
'A' => 1,
                         
'B' => 2,
                         
'C' => 3,
                         
'D' => 4,
                         
'E' => 5
                       
),
                 array (
                         
'A' => 1,
                         
'B' => 2,
                         
'C' => 3,
                         
'D' => array (
                                        array (
                                                
1,
                                                
2,
                                                
3,
                                                
4,
                                                
5
                                              
),
                                        array (
                                                
6,
                                                
7,
                                                
8,
                                                
9,
                                                
10
                                              
),
                                        array (
                                                
11,
                                                
12
                                                array (
                                                        array (
                                                                
11 => 1,
                                                                
13 => 2,
                                                                
15 => 3,
                                                                
17 => 4,
                                                                
19 => 5
                                                              
),
                                                        array ( 
                                                                
11 =>  6,
                                                                
13 =>  7,
                                                                
15 =>  8,
                                                                
17 =>  9,
                                                                
19 => 10
                                                              
),
                                                        array (
                                                                
11 => 11,
                                                                
13 => 12,
                                                                
15 => 13,
                                                                
17 => 14,
                                                                
19 => 15
                                                              
),
                                                        array (
                                                                
11 => 16,
                                                                
13 => 17,
                                                                
15 => 18,
                                                                
17 => 19,
                                                                
19 => 20
                                                              
),
                                                      ),
                                                
14,
                                                
15
                                              
),
                                        array (
                                                
16,
                                                
17,
                                                
18,
                                                
19,
                                                
20
                                              
),
                                        array (
                                                
21,
                                                
22,
                                                
23,
                                                
24,
                                                
25
                                              
)
                                      ),
                         
'E' => NULL
                       
)
               );

  
DumpArrayToTable $arT );
?>



Ausgabebeispiel: Browseransicht

ABCDE
12empty45
12345
123
01234
12345
678910
1112
1113151719
12345
678910
1112131415
1617181920
1415
1617181920
2122232425
empty






 




 sponsored by

Sedo - Bei uns wird PHP großgeschrieben


VERIO - An NTT Communications Company


HighText iBusiness


Webspace-Verkauf.de


video2brain




 Premium-Partner
 Webhosting/Serverlösungen

BPI-Systeme


Premium-Partner Pixel X


Premium-Partner First Colo


Premium-Partner dogado Internet




Qozido


© 2001-2008 E-Mail SELFPHP - Damir Enseleit, info@selfphp.deImpressumKontakt