PHP Array Debug


/ Published in: PHP
Save to your folder(s)

a more useful way of printing php arrays in a nice readable format and able to decode serialized and json_encoded strings.


Copy this code and paste it in your HTML
  1. function debug_r($array=null,$dump=false,$string_type='serialize')
  2. {
  3. echo '<pre>'."
  4. ";
  5. if(is_array($array))
  6. {
  7. if(count($array) > 0)
  8. {
  9. if($dump)
  10. {
  11. var_dump($array);
  12. }else{
  13. print_r($array);
  14. }
  15. }else{
  16. echo "Array empty"."
  17. ";
  18. }
  19. }else if(is_string($array))
  20. {
  21. switch($string_type)
  22. {
  23. default:
  24. case 'serialize':
  25. $array = unserialize($array);
  26. break;
  27. case 'json':
  28. $array = json_decode($array);
  29. break;
  30. }
  31. if(count($array) > 0)
  32. {
  33. if($dump)
  34. {
  35. var_dump($array);
  36. }else{
  37. print_r($array);
  38. }
  39. }else{
  40. echo "Array empty"."
  41. ";
  42. }
  43. }else{
  44. echo 'Invalid format'."
  45. ";
  46. }
  47. echo '</pre>'."
  48. ";
  49. }//end debug_r

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.