i need to print php array in browser console using javascript
here i shared my code
<?php
function browser_console($data) {
echo "<script>console.log('" . $data . "');</script>";
}
if anybody knows best code let me know .
thank you
The echo command is used in PHP to print any value to the HTML document. Use <script> tag inside echo command to print to the console.
You can easily use PHP array in javascript you only need to convert PHP array into JSON format Using json_encode() function. PHP array can be converted to JavScript array and accessible in JavaScript. Whatever the array type is, a single or multidimensional or indexed or associative array.
To display array structure and values in PHP, we can use two functions. We can use var_dump() or print_r() to display the values of an array in human-readable format or to see the output value of the program array.
Using print_r() function The most common method to print the contents of an array is using the print_r() function, which displays the human information about a variable. For an array, the values are displayed in a format that shows keys and elements.
I'm using
echo "<script>console.log(".json_encode(var_export($object, true)).");</script>";
as it allows you to display complex PHP objects and not only up to arrays.
Try using json_encode()
Example
echo "<script>console.log('" . json_encode($data) . "');</script>";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With