How to convert object into json string array in php

PHPz
Release: 2023-04-26 09:56:04
Original
547 people have browsed it

In web development, we often encounter the need to convert objects into JSON strings. As a commonly used back-end development language, PHP provides a convenient solution for this. This article will introduce how to convert objects into JSON string arrays to meet actual needs.

  1. Object Overview

In PHP, objects can be understood as class-based instances. A class can contain many properties and methods, which can be assigned and called when the object is created. The following is an example of a simple class:

class Person {
  public $name;
  public $age;

  function __construct($name, $age) {
    $this->name = $name;
    $this->age = $age;
  }
}
Copy after login

When using this class, you can create a Person object and assign values ​​to its name and age attributes:

$person = new Person('张三', 25);
Copy after login
  1. PHP built-in Method json_encode()

PHP provides the built-in method json_encode(), which can convert PHP values ​​into JSON format strings.

For example, you can use json_encode() to convert a PHP array into a JSON string:

$fruits = array('apple', 'banana', 'orange');
echo json_encode($fruits);
// 输出:["apple","banana","orange"]
Copy after login

When using json_encode(), you need to pay attention to the following points:

  • The object must have at least one public property as a property of the JSON object.
  • Private properties, protected properties and static properties will be ignored.
  • json_encode() can only handle UTF-8 encoded data.
  • Methods in the object will not be converted to JSON data.
  1. Convert object to array

Before converting the object to a JSON string, you need to convert the object to an array first. PHP provides the built-in method get_object_vars(), which can convert objects into associative arrays. For example:

$person = new Person('张三', 25);
$personArray = get_object_vars($person);
print_r($personArray);
// 输出:Array ( [name] => 张三 [age] => 25 )
Copy after login
  1. Convert to JSON string

Using the json_encode() and get_object_vars() methods, objects can be converted to JSON strings. Here is the sample code:

class Person {
  public $name;
  public $age;

  function __construct($name, $age) {
    $this->name = $name;
    $this->age = $age;
  }

  function toArray() {
    return get_object_vars($this);
  }
}

$person1 = new Person('张三', 25);
$person2 = new Person('李四', 30);

$personArray1 = $person1->toArray();
$personArray2 = $person2->toArray();

$people = array($personArray1, $personArray2);

$peopleJson = json_encode($people);

echo $peopleJson;
// 输出:[{"name":"张三","age":25},{"name":"李四","age":30}]
Copy after login

In the above example, two Person objects are first created and converted into arrays. These arrays are then put into a PHP array and finally the array is converted to a JSON string using the json_encode() method.

  1. Summary

This article explains how to convert an object into a JSON string array in PHP. By using the get_object_vars() method to convert the object into an array, and then using the json_encode() method to convert the array into a JSON string, you can quickly and easily meet real-world needs. It should be noted that when using the json_encode() method, pay attention to the definition of the class and the visibility of the attributes.

The above is the detailed content of How to convert object into json string 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!