


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
- 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);
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 )
- 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;
The above code will convert the JSON string into PHP object and output the content of the object. The output result is as follows:
张三
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. .
- 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);
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 ) )
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Chinese version
Chinese version, very easy to use

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool
