i have written code in php file that to connect to database and get the requested data .
  <?php
 // 1. Create a database connection
 $dbhost = "localhost";
 $dbuser = "root";
 $dbpass = "root";
 $dbname = "widget_corp";
   $connection = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
  // Test if connection succeeded
   if(mysqli_connect_errno()) {
     die("Database connection failed: " . 
     mysqli_connect_error() . 
     " (" . mysqli_connect_errno() . ")"
   );
 }
       $query  = "SELECT * ";
       $query .= "FROM subjects ";
       $query .= "WHERE visible = 1 ";
       $query .= "ORDER BY position ASC";
       $result = mysqli_query($connection, $query);
       $subject = mysqli_fetch_assoc($result);
      print_r($subject);
  ?>
i am getting the output in array format ,i want to get the out put in json format.how to change the code ,please help me .