Home  >  Article  >  Backend Development  >  How to convert php to arrays and objects

How to convert php to arrays and objects

PHPz
PHPzOriginal
2023-04-25 10:43:01402browse

In PHP, arrays and objects are two common data structures that play an important role in data processing. When we need to convert a data source into an array or object, we often need to use some corresponding functions or methods to achieve it. The following will explore in detail the related techniques for converting data sources into arrays and objects in PHP.

1. PHP converts an array into an object

In PHP, the method to convert an array into an object is very simple, we only need to use forced type conversion. An example is as follows:

$array = [
    'name' => '张三',
    'age' => 20
];

$object = (object) $array;

echo $object->name;  //输出:张三
echo $object->age;   //输出:20

By casting the array, PHP will automatically use the key names in the array as object attributes and the key values ​​as attribute values. This method is very simple and convenient, and is suitable for the conversion of small data sources.

2. Convert an object to an array

In contrast to converting an array to an object, PHP's method of converting an object to an array is relatively simple. We can use the get_object_vars() or json_decode() function to achieve this. The specific operations of these two methods are introduced below.

  1. get_object_vars() function

The get_object_vars() function can convert an object into an array and return an associative array composed of object attributes. An example is as follows:

class Person {
    public $name = '张三';
    protected $age = 20;
    private $gender = '男';

    function __construct() {
        echo '对象已创建';
    }
}

$person = new Person();

$array = get_object_vars($person);

print_r($array);

After running the above code, the output result is as follows:

对象已创建
Array
(
    [name] => 张三
    [age] => 20
    [*gender] => 男
)

As you can see from the results, the get_object_vars() function converts all the attributes in the object into array elements. However, it should be noted that if the object contains private properties or protected properties, they will be marked as weak variables and include an asterisk (*) in the output.

  1. json_decode() function

json_decode() function is one of the commonly used functions in PHP, which can convert JSON format strings into PHP arrays or objects. When we need to convert an object into an array, we can first convert the object into a JSON format string and then parse it through the json_decode() function. The sample code is as follows:

class Person {
    public $name = '张三';
    protected $age = 20;
    private $gender = '男';

    function __construct() {
        echo '对象已创建';
    }
}

$person = new Person();

$json = json_encode($person);

$array = json_decode($json, true);

print_r($array);

After executing the above code, the output is as follows:

对象已创建
Array
(
    [name] => 张三
    [age] => 20
)

As can be seen from the results, the json_decode() function can convert all public attributes in the object into array elements, and All private and protected properties are ignored. If you need to convert private and protected properties to array elements as well, you can set the second parameter to false.

3. Summary

The above are the related technologies for converting data sources in PHP into arrays and objects. When we need to analyze and process a certain data source, it is very important to choose the appropriate data structure. Converting data sources into arrays or objects facilitates friendly and standardized data processing. I hope that the content described in this article will be helpful to the development work of the majority of PHP developers.

The above is the detailed content of How to convert php to arrays and objects. 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