Home  >  Article  >  Backend Development  >  How to convert php json into array object

How to convert php json into array object

PHPz
PHPzOriginal
2023-04-18 09:47:31495browse

In web development, it is often necessary to convert JSON format strings into array objects to facilitate data processing. In PHP, we can accomplish this task through built-in functions. This article will introduce how to convert JSON into an array object in PHP.

First of all, we need to understand the basic structure and purpose of JSON format. JSON (JavaScript Object Notation) is a lightweight data exchange format expressed in text format that can be used for data exchange between various applications. Its basic structure is a key-value pair, separated by commas, using curly brackets to represent an object, and square brackets to represent an array.

The sample JSON format string is as follows:

{
   "name": "Lucy",
   "age": 25,
   "hobbies": ["reading", "swimming", "traveling"],
   "address": {
       "country": "China",
       "city": "Beijing",
       "postal_code": "100000"
   }
}

In PHP, we can use the json_decode() function to convert the JSON string into an array object. The first parameter of this function is the JSON string to be converted, and the second parameter is used to set the converted data type. For example, we can set the second parameter to true to convert it to an associative array; we can also set it to false to convert it to an ordinary object array. The sample code is as follows:

$json_str = '{
   "name": "Lucy",
   "age": 25,
   "hobbies": ["reading", "swimming", "traveling"],
   "address": {
       "country": "China",
       "city": "Beijing",
       "postal_code": "100000"
   }
}';

$arr = json_decode($json_str, true);
var_dump($arr);

The output result is:

array(4) {
  ["name"]=>
  string(4) "Lucy"
  ["age"]=>
  int(25)
  ["hobbies"]=>
  array(3) {
    [0]=>
    string(7) "reading"
    [1]=>
    string(8) "swimming"
    [2]=>
    string(9) "traveling"
  }
  ["address"]=>
  array(3) {
    ["country"]=>
    string(5) "China"
    ["city"]=>
    string(7) "Beijing"
    ["postal_code"]=>
    string(6) "100000"
  }
}

As you can see, we convert the JSON string into an array object by using the json_decode() function. Among them, name and age are ordinary key-value pairs, and hobbies and address represent a nested array and object respectively. In the converted array, we can access the corresponding value through the corresponding key name.

In addition, we can also convert JSON strings into objects. The sample code is as follows:

$json_str = '{
   "name": "Lucy",
   "age": 25,
   "hobbies": ["reading", "swimming", "traveling"],
   "address": {
       "country": "China",
       "city": "Beijing",
       "postal_code": "100000"
   }
}';

$obj = json_decode($json_str);
var_dump($obj);

The output result is:

object(stdClass)#1 (4) {
  ["name"]=>
  string(4) "Lucy"
  ["age"]=>
  int(25)
  ["hobbies"]=>
  array(3) {
    [0]=>
    string(7) "reading"
    [1]=>
    string(8) "swimming"
    [2]=>
    string(9) "traveling"
  }
  ["address"]=>
  object(stdClass)#2 (3) {
    ["country"]=>
    string(5) "China"
    ["city"]=>
    string(7) "Beijing"
    ["postal_code"]=>
    string(6) "100000"
  }
}

We can see that after conversion The result is a stdClass object, which is an ordinary PHP object. We can also access the corresponding value through the corresponding attribute name.

In short, in PHP, we can easily process data by using the built-in json_decode() function to convert JSON-formatted strings into array objects or ordinary PHP objects. In actual Web development, processing data in JSON format has become very common, so mastering the relevant knowledge of JSON conversion has become one of the necessary skills.

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

Statement:
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