Convert php array to Javascript
Ankit Verma

Ankit Verma @ankitvermaonline

About: Full Stack Developer 7+ years of experience in Web development, specializing in PHP frameworks.

Location:
Lucknow
Joined:
Sep 18, 2023

Convert php array to Javascript

Publish Date: May 27
0 0
<?php
$user = [
    'name' => 'John Doe',
    'email' => 'john@example.com',
    'is_admin' => true,
    'age' => 30
];
?>
Enter fullscreen mode Exit fullscreen mode
<script type="text/javascript">
    // Assign PHP user array to a JS variable
    var user = <?php echo json_encode($user); ?>;
    console.log(user);
</script>
Enter fullscreen mode Exit fullscreen mode

Output in JavaScript:

{
  name: "John Doe",
  email: "john@example.com",
  is_admin: true,
  age: 30
}
Enter fullscreen mode Exit fullscreen mode

Comments 0 total

    Add comment