Posted By

PapTom on 02/10/09


Tagged

debug array tool


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

PapTom
Hilyin
BrianCoyDesign


Print array


 / Published in: PHP
 

Function printing the contents of an array. Similar to var_dump but better looking. As it shows a table... Great for debug.

  1. function printArray($arrayToPrint)
  2. {
  3. if(is_array($arrayToPrint)==true)
  4. {
  5. $i=1;
  6. echo "<table border=1>";
  7. $prevFieldNameStr="";
  8. foreach ($arrayToPrint as $key=>$row) //gia kathe row...
  9. {
  10. $fieldNameStr="";
  11. $fieldValueStr="";
  12. echo "\n<tr>";
  13. if (count($row)>1)
  14. {
  15. foreach ($row as $fieldName=>$fieldValue) //gia kathe field...
  16. {
  17. if (is_array($fiedValue))
  18. {
  19. foreach ($fieldValue as $f=>$v)
  20. {
  21. $fieldNameStr.="<td><b>" . $f . "</b></td>";
  22. $fieldValueStr.="<td>" . $v. "</td>";
  23. }
  24. }
  25. else
  26. {
  27.  
  28. $fieldNameStr.="<td><b>" . $fieldName . "</b></td>";
  29. $fieldValueStr.="<td>" . $fieldValue. "</td>";
  30. }
  31. }
  32. }
  33. else
  34. {
  35. $fieldValueStr.="<td>" . $row. "</td>";
  36. }
  37.  
  38. //prints fieldNames only if they are different
  39. if (strcmp($fieldNameStr,$prevFieldNameStr)==0)
  40. {
  41. }
  42. else
  43. {
  44. echo "<td>A/A</td>";
  45. echo $fieldNameStr;
  46. $prevFieldNameStr=$fieldNameStr;
  47. }
  48. echo "</tr>\n<tr>";
  49. echo "<td>$key</td>". $fieldValueStr;
  50. echo "</tr>";
  51. }
  52. echo "</table>\n";
  53. }
  54. else
  55. {
  56. throw new Exception("The given parameter is not an array");
  57. }
  58. }

Report this snippet  

You need to login to post a comment.