login  Naam:   Wachtwoord: 
Registreer je!
 Scripts:

Scripts > PHP > Handige scripts > [PHP] array_dump functie

[PHP] array_dump functie

Auteur: Stijn - 04 augustus 2006 - 02:23 - Gekeurd door: Thomas - Hits: 3626 - Aantal punten: 2.83 (3 stemmen)




Ik ben het beu om var_dump(); te gebruiken als het gaat om array's. Daarom heb ik me eigen functie gemaakt die je array overzichtelijker maakt en goed voor de ogen is.

Dit is handig voor beginners als experts want of het nu simpele of geneste array's gaat is dit de manier om je array op een nette manier te bekijken!

bekijk het voorbeeld als je niet weet waarover ik het heb 

veel plezier ermee , stijn

de afbeeldingen vind je http://dhost.info/webcreator/scripts/images/ of in de map 'images' bij de download.

Code:
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3. /**
  4.  * a function to view your array in detail. you don't need to worry about $space. it's for the layout.
  5.  *
  6.  * @license http://www.gnu.org/licenses/gpl.html
  7.  * @author stijn1989 <stijnleenknegt@gmail.com>
  8.  * @version Versie 1.0
  9.  * @package PHP
  10.  */
  11.  
  12. // full error reporting
  13.  
  14. /**
  15.  * this is a function that gives you big details of your array
  16.  *
  17.  * @param array $array
  18.  * @param integer $space
  19.  * @return void
  20.  */
  21. function array_dump( $array , $space = 0 )
  22. {
  23.  
  24. if( is_array( $array ) === false ) {
  25. die('You must give an array type as parameter for this function!');
  26. }
  27.  
  28. //show the branch
  29. if( $space != 0 ) {
  30. echo "<img src=\"images/tree_1.gif\">";
  31. for( $i = 0 ; $i < $space ; $i++ ) {
  32. echo "<img src=\"images/tree_4.gif\">";
  33. }
  34. }
  35. echo "<img src=\"images/branch.png\">";
  36. echo " ";
  37. echo "<b>Array: </b>";
  38. echo count($array)." keys/values";
  39. echo "<br />";
  40.  
  41. //show the leafs
  42. $c = 1;
  43. foreach($array as $k => $v) {
  44.  
  45. if( is_array( $v ) === true ) {
  46. array_dump($v , $space + 1);
  47. } else {
  48. if( $space != 0 ) {
  49. echo "<img src=\"images/tree_3.gif\">";
  50. for( $i = 0 ; $i < $space ; $i++ ) {
  51. echo "<img src=\"images/space.gif\">";
  52. }
  53. }
  54. echo ( count($array) == $c ) ? "<img src=\"images/tree_2.gif\">" : "<img src=\"images/tree_1.gif\">";
  55. echo "<img src=\"images/leaf.png\">";
  56. echo " ";
  57. echo "key: <b>" . $k . "</b>";
  58. echo " <b>-</b> ";
  59. echo "value: <b>" . $v . "</b>";
  60. echo " <b>-</b> ";
  61. echo "type: <b>" . gettype( $v ) ."</b>";
  62. echo "<br />";
  63. }
  64. $c++;
  65.  
  66. }
  67.  
  68. }
  69. ?>
Download code! Download code (.txt)

Download dit script! Bekijk een voorbeeld van dit script!
 Stemmen
Niet ingelogd.

 Reacties
Post een reactie
Lees de reacties (5)
© 2002-2024 Sitemasters.be - Regels - Laadtijd: 0.022s