php print array in console code example

Example 1: print array php

<?php
$a = array ('a' => 'apple', 'b' => 'banana', 'c' => array ('x', 'y', 'z'));
echo "<pre>";
print_r ($a);
echo "</pre>";
?>
  
Output:

Array
(
    [a] => apple
    [b] => banana
    [c] => Array
        (
            [0] => x
            [1] => y
            [2] => z
        )
)

Example 2: php echo and array to consle

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

Example 3: php echo an array to console

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

Example 4: php array to console

function console_log( $data ){
  echo '<script>';
  echo 'console.log('. json_encode( $data ) .')';
  echo '</script>';
}

Usage:
$myvar = array(1,2,3);
console_log( $myvar ); // [1,2,3]

Tags:

Php Example