How to convert json to object array in php

PHPz
Release: 2023-04-19 15:17:34
Original
389 people have browsed it

With the development of the Internet, Web development has become more and more popular. Among them, PHP is a very popular server-side programming language that is often used to develop web applications. The PHP language is not only easy to learn and use, but it is also very flexible and capable of performing various types of programming tasks. When developing web applications, it is often necessary to serialize and deserialize data. Among them, JSON is a popular data exchange format capable of communicating between different applications. In the PHP language, there is a very convenient method to convert a JSON string into an array of PHP objects, which is convenient for us to process in the program. Let's learn this method together.

First, let us understand what JSON is. JSON (JavaScript Object Notation) is a lightweight data exchange format. Data in the JSON data format is represented by key-value pairs, where keys and values ​​are separated by colons (:), and key-value pairs are separated by commas (,). In the JSON data format, curly braces ({}) are used to represent objects, and square brackets ([]) are used to represent arrays. The following is a simple JSON string:

{
    "name": "Tom",
    "age": 18,
    "hobbies": ["reading", "swimming"]
}
Copy after login

In the PHP language, we can use the json_decode function to convert a JSON string into an array of PHP objects. The syntax of this function is as follows:

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

Parameter description:

  • $json: JSON string that needs to be decoded.
  • $assoc: An optional Boolean parameter, the default is false, indicating the return object type. If set to true, an array type is returned.
  • $depth: An optional integer parameter indicating the depth of recursive decoding. The default value is 512.
  • $options: An optional integer parameter indicating decoding options. The default is 0, which means there are no options.

The following is a simple example that demonstrates how to use the json_decode function to decode a JSON string into an array of PHP objects:

<?php
$json_string = &#39;{
    "name": "Tom",
    "age": 18,
    "hobbies": ["reading", "swimming"]
}&#39;;

$json_obj = json_decode($json_string);

var_dump($json_obj);

?>
Copy after login

The above code will return the following results:

object(stdClass)#1 (3) {
  ["name"]=>
  string(3) "Tom"
  ["age"]=>
  int(18)
  ["hobbies"]=>
  array(2) {
    [0]=>
    string(7) "reading"
    [1]=>
    string(8) "swimming"
  }
}
Copy after login

In the above example, we first define a JSON string. We then use the json_decode function to decode that string into an array of PHP objects. Finally, we use var_dump to print out the details of the object array. As can be seen from the output, $json_obj is a PHP object that contains all key-value pairs in the JSON string.

If we need to decode a JSON string into a PHP array, we only need to set the second parameter $assoc of the json_decode function to true. The following is the corresponding code example:

<?php
$json_string = &#39;{
    "name": "Tom",
    "age": 18,
    "hobbies": ["reading", "swimming"]
}&#39;;

$json_arr = json_decode($json_string, true);

var_dump($json_arr);

?>
Copy after login

The above code will return the following results:

array(3) {
  ["name"]=>
  string(3) "Tom"
  ["age"]=>
  int(18)
  ["hobbies"]=>
  array(2) {
    [0]=>
    string(7) "reading"
    [1]=>
    string(8) "swimming"
  }
}
Copy after login

As can be seen from the output results, $json_arr is a PHP array, which contains the JSON string All key-value pairs.

In summary, using the json_decode function can easily convert a JSON string into a PHP object array or PHP array. The use of this function is very simple, you only need to provide the JSON string that needs to be decoded. In actual web applications, we often need to use JSON as a data exchange format. Therefore, it is very important to master the method of using the json_decode function, which can help us better manage and process data.

The above is the detailed content of How to convert json to 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!