Convert json string to json array php

WBOY
Release: 2023-05-19 21:01:06
Original
1229 people have browsed it

In PHP development, we often need to process JSON strings, and sometimes we need to convert JSON strings into JSON arrays. Today we will learn how to convert JSON string to JSON array in PHP.

  1. Use the json_decode() function

PHP provides a very simple function - json_decode(), which is used to convert JSON strings into PHP objects or arrays.

Syntax:

mixed json_decode ( string $json [, bool $assoc = FALSE [, int $depth = 512 [, int $options = 0 ]]] )
Copy after login

Parameters:

  • json: JSON string that needs to be decoded.
  • assoc: When this parameter is TRUE, array will be returned instead of object.
  • depth: Set the maximum depth. The default allowed depth is 512, if this value is exceeded, NULL will be returned.
  • options: Binary mask, affecting decoding behavior. Commonly used options include JSON_HEX_QUOT, JSON_HEX_TAG, JSON_HEX_AMP, JSON_HEX_APOS, JSON_NUMERIC_CHECK, JSON_PRETTY_PRINT, JSON_UNESCAPED_SLASHES, JSON_UNESCAPED_UNICODE, etc. For details, please refer to the PHP official documentation.

Example:

$json_string = '{"name": "Tom","age": 18,"gender": "male"}'; $json_array = json_decode($json_string, true); print_r($json_array);
Copy after login

Output:

Array ( [name] => Tom [age] => 18 [gender] => male )
Copy after login

In the above example, $json_array is a JSON array.

  1. Use json_decode() to parse two-dimensional arrays

If the JSON string contains a two-dimensional array, we can set the assoc parameter to false and then pass json_decode( ) function parses to get an object of type stdClass, and then you can use the object properties to get the value.

Example:

$json_string = '[{"name": "Tom","age": 18,"gender": "male"},{"name": "Alice","age": 20,"gender": "female"}]'; $json_array = json_decode($json_string, false); echo $json_array[0]->name;
Copy after login

Output:

Tom
Copy after login

In the above example, $json_array[0]->name is the name attribute of the first element in the JSON array value.

  1. Convert JSON string to PHP object

If we want to convert JSON string to PHP object instead of array, we can set the assoc parameter to false , or do not pass this parameter.

Example:

$json_string = '{"name": "Tom","age": 18,"gender": "male"}'; $json_object = json_decode($json_string); echo $json_object->age;
Copy after login

Output:

18
Copy after login

In the above example, $json_object is a PHP object, and we can use object properties to get the value.

Summary

This article introduces two methods of converting JSON strings to JSON arrays in PHP: using the json_decode() function to parse key-value pair arrays and multi-dimensional arrays, and converting JSON characters Convert string to PHP object.

No matter which method is used, you can convert the JSON string into a JSONArray, and then use PHP to obtain the JSON data, which will help us process the data better and develop better applications.

The above is the detailed content of Convert json string to json array 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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!