Home > Backend Development > PHP Problem > How to convert object data into array in php

How to convert object data into array in php

PHPz
Release: 2023-04-23 19:40:45
Original
776 people have browsed it

In PHP, objects and arrays are commonly used data types. But when processing data, you need to convert an object into an array. This conversion is very common in PHP and the process is simple. In this article, we will explain how to convert an object into an array.

PHP objects are based on class instances, and each object has its own properties and methods. The properties of an object usually contain some basic types of values, such as numbers, strings, and Boolean values, but they can also be other objects. The properties and methods of an object can be accessed by using the arrow symbol "->".

An array, on the other hand, is a container data type that contains a set of values ​​arranged in a specific order. Arrays can contain any type of data, including numbers, strings, Boolean values, objects, and other arrays.

To convert PHP objects into arrays, PHP provides two built-in functions: get_object_vars() and json_decode(). Below we will introduce the usage of these two functions in detail.

Use the get_object_vars() function

The get_object_vars() function accepts an object as a parameter and returns an associative array of the object's attributes and corresponding values. The keys of this array are the names of the object's properties, and the values ​​are the values ​​of the properties.

The following is a simple example:

class Person {
  public $name = "John";
  public $age = 30;
  public $city = "New York";
}

$person = new Person();
$personArray = get_object_vars($person);
print_r($personArray);
Copy after login

The output result is:

Array ( [name] => John [age] => 30 [city] => New York )
Copy after login
Copy after login

As shown above, the array returned by the get_object_vars() function contains all attributes of the Person class and the corresponding value, while the parameter of the function is the object itself.

Use the json_decode() function

The json_decode() function converts JSON format data into a PHP object or array. Its first parameter is a JSON-formatted string, and the second parameter is a Boolean value that specifies whether to convert the JSON string into an associative array.

The following is a simple example:

$jsonString = '{"name":"John","age":30,"city":"New York"}';
$personArray = json_decode($jsonString, true);
print_r($personArray);
Copy after login

The output result is:

Array ( [name] => John [age] => 30 [city] => New York )
Copy after login
Copy after login

As shown above, we first create a JSON format string. We then use the json_decode() function to convert this string into an array. In the function call, the second parameter is set to true, which means we want to convert the JSON string to an associative array.

Note: By default, the json_decode() function will convert a JSON string to a PHP object, not an array. If you want to convert a JSON string to an array, set the second parameter of the function parameter to true.

Summary

Converting an object to an array is very simple in PHP and can be done using two built-in functions. The get_object_vars() function converts object properties into arrays, while the json_decode() function converts JSON strings into arrays. Both methods convert objects into arrays.

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