Print or Display PHP array in Browser Console or in Javascript

Try adding JSON.parse in addition to what the others said - that way it gets console.logged as an object, meaning you can expand it and navigate it in Chrome like so.

 echo "<script>console.log(JSON.parse('" . json_encode($data) . "'));</script>";

screenshot of expanded object echoed like above

Source: https://alligator.io/js/json-parse-stringify/


Note: I understand this question is asking about arrays, so string is probably fine, but to represent more complex objects this works better.


Try using json_encode()

Example

echo "<script>console.log('" . json_encode($data) . "');</script>";