The Web Blinders logo

Programming

PHP Save Array as a JSON File and JSON File to PHP Array

<?php

$arr=[
    "user"=>"root",
    "port"=>3306,
    "database"=>"home"
];

// Converting to JSON and saving it in a file
file_put_contents('t.json',json_encode($arr));

// Testing , if the file is created or not
echo file_get_contents('t.json');

// {"user":"root","port":3306,"database":"home"}

// Read file and convert json to array
$a = json_decode(file_get_contents('t.json'),true);

print_r($a);
/*
Array
(
    [user] => root
    [port] => 3306
    [database] => home
)
*/
?>

ALTERNATE TITLES

Convert array to json file php

Convert json file to php array

file_put_contents() example php

file_get_contents() example php

Need developers ?

if so, send a message.

thewebblinders@gmail.com

More Programming from our blog

SEARCH FOR ARTICLES