Home > Backend Development > PHP Problem > How to read json file into array in php

How to read json file into array in php

青灯夜游
Release: 2023-03-15 22:30:01
Original
3278 people have browsed it

How to read json files into arrays in php: 1. Use the "file_get_contents('file path')" statement to read and store the contents of the json file into a string; 2. Use "json_decode (json string, true)" statement to convert json data into an array.

How to read json file into array in php

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

php reads json Method of converting files into arrays

Implementation idea:

  • Read the json file and read the data of the file content into a string Save

  • Convert json string to array

Implementation method:

  • Use file_get_contents(): Read the json file into a string

  • json_decode(): Convert the json string into an array

    json_decode() function can Convert JSON encoded string to appropriate PHP data type.

    By default, the json_decode() function will return an object; however, when the second parameter is specified as a Boolean value true, the JSON value will be decoded into an associative array.

Implementation example:

There is such a test.json file, the content of which is:

How to read json file into array in php

Convert it to a PHP array:

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);   
$json_string=file_get_contents(&#39;test.json&#39;);
echo $json_string;
$arr=json_decode($json_string,true);   //将json字符串转成php数组
var_dump($arr);
?>
Copy after login

How to read json file into array in php

Description: file_get_contents() function

file_get_contents () function can read the contents of a file into a string, syntax:

file_get_contents($filename,$include_path,$context,$offset,$max_length)
Copy after login

Parameter description is as follows:

  • $filename: the file name of the file to be read Name;

  • $use_include_path: optional parameter, used to set whether you want to search for the file in include_path, the default is FALSE;

  • $context: optional parameter, used to represent a valid context resource created using the stream_context_create() function. If you do not need to customize the context, you can use NULL to ignore it;

  • $offset : Optional parameter, used to set the starting position in the file to read. Note that this parameter cannot be used for remote files;

  • $maxlen: Optional parameter, used to set the number of bytes to be read. The default is to read the entire content of the file.

Note: When the file_get_contents() function fails to execute, it may return FALSE of Boolean type, or it may return a non-Boolean value (such as a null character). Therefore, the === operator is generally used to test the return value of this function.

Extended knowledge:

In addition to reading the file into a string, you can also read the entire file into an array, just use file( ) function. If you want to simply understand this function, you can parameterize the article "How to store the entire file data into an array line by line in php"

Recommended study: "PHP Video Tutorial"

The above is the detailed content of How to read json file into array in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template