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

How to convert object to json string array in php

PHPz
PHPzOriginal
2023-04-12 09:23:04830browse

As the PHP language continues to improve, processing JSON has become a very common task in PHP applications. This data format is both readable and easy to process and transmit, making it suitable for a variety of application scenarios from traditional web applications to modern technologies such as REST APIs.

When we develop PHP, we often need to convert objects into JSON string arrays to meet the needs of back-end data transmission and use. This article will introduce how to use PHP to convert objects into JSON string arrays.

1. PHP’s JSON function

To convert an object into a JSON string array, you first need to understand PHP’s built-in JSON function. These functions provide us with a very convenient interface, which can convert various data formats into JSON strings and JSON strings into various data formats. It also supports custom data format conversion rules.

1.json_encode()

The json_encode() function is one of the most basic JSON functions in PHP, used to convert data format into JSON string. It accepts a PHP array or object as parameter and converts it into a JSON string. For example:

<?php
$data = array(&#39;name&#39; => 'Tom', 'age' => 18);
echo json_encode($data);
?>

Execute this code, the output result is:

{"name":"Tom","age":18}

As you can see, the json_encode() function converts the data in array format into a JSON string.

2.json_decode()

The json_decode() function is a function that converts a JSON string into a PHP array or object. For example:

Execute this code, the output result is:

stdClass Object ( [name] => Tom [age] => 18 )

As you can see, the json_decode() function converts the JSON string into a PHP object.

2. Convert objects into JSON string arrays

After understanding the basic JSON functions, we can start converting objects into JSON string arrays.

1. Convert the object into an associative array

First, we need to convert the object into an associative array, which can be achieved through PHP's built-in get_object_vars() function. This function accepts an object as a parameter and returns an associative array representation of the object. For example:

<?php
class User {
  public $name;
  public $age;
  function __construct($name, $age) {
    $this->name = $name;
    $this->age = $age;
  }
}
$user = new User('Tom', 18);
$data = get_object_vars($user);
print_r($data);
?>

Execute this code, the output result is:

Array ( [name] => Tom [age] => 18 )

As you can see, the get_object_vars() function returns an associative array containing object attributes.

2. Convert the associative array into a JSON string

Next, we will use the json_encode() function to convert the associative array into a JSON string. For example:

<?php
class User {
  public $name;
  public $age;
  function __construct($name, $age) {
    $this->name = $name;
    $this->age = $age;
  }
}
$user = new User('Tom', 18);
$data = get_object_vars($user);
$json_str = json_encode($data);
echo $json_str;
?>

Execute this code, the output result is:

{"name":"Tom","age":18}

As you can see, we successfully converted the associative array into a JSON string.

3. Summary

Through the above methods, we can easily convert objects into JSON string arrays, thereby meeting the data transmission and use needs of PHP applications. In the actual use process, we can also conduct more in-depth study and application of JSON functions to achieve more functions that are actually needed.

The above is the detailed content of How to convert object to json string array in php. 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