How to convert JSON format data into an array or object array in php

PHPz
Release: 2023-04-24 16:30:45
Original
592 people have browsed it

In PHP development, data processing in JSON format is very common. I believe many developers have encountered situations where they need to convert JSON objects into arrays or object arrays. This article will introduce how to convert JSON format data into an array or object array.

1. Introduction to JSON format

JSON (JavaScript Object Notation) is a lightweight data exchange format. It is easy for humans to read and write, and easy for machines to parse and generate. Data in JSON format is stored and transmitted in the form of key-value pairs, so it is also regarded as a data type similar to objects and arrays in JavaScript.

2. Processing of JSON data in PHP

  1. Convert JSON data to PHP array

PHP provides a very convenient function json_decode( ), you can convert data in JSON format into a PHP array. The specific usage is as follows:

$json = '{"name":"张三","age": 18}';
$people = json_decode($json, true);
print_r($people);
Copy after login

The above code will convert the JSON string into a PHP array and output the contents of the array. The output result is as follows:

Array
(
    [name] => 张三
    [age] => 18
)
Copy after login
  1. Convert JSON data into a PHP object

If you need to convert JSON format data into a PHP object, you can use the default options of the json_decode() function, as follows:

$json = '{"name":"张三","age": 18}';
$people = json_decode($json);
echo $people->name;
Copy after login

The above code will convert the JSON string into PHP object and output the content of the object. The output result is as follows:

张三
Copy after login

It should be noted that if the second parameter of json_decode() is not specified, a PHP object is returned, and an object of stdClass type is returned by default. .

  1. Convert JSON data into a PHP object array

If you need to convert JSON data into a PHP object array, you can first convert the JSON data into a PHP array, and then pass Convert a PHP array into an array of PHP objects by looping through it (or use the array_map() function to convert):

$json = '[{"name":"张三","age": 18},{"name":"李四","age": 20}]';
$people = json_decode($json, true);
$peopleList = [];
foreach ($people as $person) {
    $peopleList[] = (object)$person;
}
print_r($peopleList);
Copy after login

The above code will convert the JSON string into an array of PHP objects and output the contents of the array and output the result. As follows:

Array
(
    [0] => stdClass Object
        (
            [name] => 张三
            [age] => 18
        )

    [1] => stdClass Object
        (
            [name] => 李四
            [age] => 20
        )

)
Copy after login

It should be noted that when coercing PHP array elements into objects, you need to use the (object) type conversion character.

3. Summary

Through the introduction of this article, everyone should be able to master the method of converting JSON format data into a PHP array or object array. In actual development, we usually need to process the JSON data obtained from the API interface, and then perform related business logic operations. Therefore, having good JSON data processing capabilities is particularly important for PHP developers.

The above is the detailed content of How to convert JSON format data into an array or object array in php. For more information, please follow other related articles on the PHP Chinese website!

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