Elias Elias - 6 months ago 15
PHP Question

Best way to debug an array using PHP

What is the best way to debug an array so that you can see what values are being stored and in what keys in the array they are being stored at? Also how do you make it so that it's easier to look at visually so that you don't have to keep looking through the array for the key and it's value in the one line

print_r()
function?

EDIT:

I now realize that
print_r()
is not the only solution to debugging arrays. So if you have alternate solutions that would be lovely as well to learn more about debugging.

EDIT2:

Ayesh K, ITroubs and Robert Rozas have mentioned both Krumo and Kint this far, if you have others feel free to post them. Also thanks to Raveren for writing Kint!

Answer

Everyone suggests print_r which is in core and works really well. But when it comes to view a large array, print_r() drives me nuts narrowing down the output.

Give a try to krumo. It nicely prints the array with visual formatting, click-expand and it also gives you the exact array key call that you can simply copy and paste.

<?php
 krumo($my_array);
?>

Itroubs mentioned Kint as a better alternative to Krumo. (Thanks ITroubs!)