Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How php reads json files into arrays

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "php how to read json files into arrays", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's take a small series to take you to learn "php how to read json files into arrays"!

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

Operating environment of this tutorial: Windows 7 system, PHP7.1 version, DELL G3 computer

php read json file into array method

Realization of ideas:

Read the json file and save the data of the file content into a string

Convert json string to array

Implementation method:

file_get_contents(): reads the json file into a string

json_decode(): Convert json string to array

The json_decode() function converts a JSON-encoded string to the appropriate PHP data type.

By default, the json_decode() function returns an object; however, when the second argument is specified as a Boolean value of true, the JSON value is decoded into an associative array.

Implementation example:

There is such a test.json file, which contains the following contents:

Turn it into a PHP array:

file_get_contents() function

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

file_get_contents($filename,$include_path,$context,$offset,$max_length)

Parameters are described as follows:

$filename: the name of the file to read;

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

$context: optional parameter, used to indicate a valid context resource created using the stream_context_create() function, if no custom context is needed, NULL can be used to ignore it;

$offset: Optional parameter used to set the position in the file to start reading. Note that this parameter cannot be used for remote files;

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

Note: When the file_get_contents() function fails, it may return FALSE of Boolean type or a non-Boolean value (such as null). So the return value of this function is generally tested using the === operator.

At this point, I believe that we have a deeper understanding of "how php reads json files into arrays", so let's actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report