Hackerss.com

hackerss
hackerss

Posted on

Php Read a json file and display all the data on the json


<?php

/**
 * function to read a json file and display all the data on the json with comments
 * @param string $file_name  Name of the json file
 * @return $data
 */ 
function readJsonFile($file_name) {
  // read the json file
  $data = file_get_contents($file_name);
  // decode the json data into an associative array
  $data = json_decode($data, true);
  // return the data
  return $data;
}

//example
$result = readJsonFile('data.json');

//output
echo $result; //{"name":"John","age":30,"city":"New York"}


Enter fullscreen mode Exit fullscreen mode

Top comments (0)