Home >Backend Development >PHP Problem >How to get objects and convert data types in PHP

How to get objects and convert data types in PHP

PHPz
PHPzOriginal
2023-04-18 15:21:57709browse

PHP is a commonly used back-end development language, which is often used to develop web applications. In PHP, we often operate objects and arrays, and we often need to convert objects into JSON strings or JSON strings into objects or arrays. In this article, we will explain how to get an object in PHP and convert it to different data types like JSON string, array, array of objects, etc.

Get the object

You can use the new keyword to create an object in PHP. For example, we can use the following code to create a Person object named $person:

class Person {
    public $name;
    public $age;
}

$person = new Person();
$person->name = '张三';
$person->age = 21;

The above code defines a class named Person, which has Two public properties name and age. After that, we create a Person object using the new keyword and store it in the $person variable. We set the values ​​of the name and age attributes to the object, which are 'Zhang San' and 21 respectively.

Convert an object to a JSON string

In PHP, we can use the json_encode() function to convert an object to a JSON string. For example, we can use the following code to convert the $person object to a JSON string:

$person_json = json_encode($person);

The above code converts the $person object to a JSON string and The results are stored in the $person_json variable. The result is as follows:

{"name":"张三","age":21}

Convert JSON string to array

In PHP, we can use the json_decode() function to convert a JSON string into an array. For example, we can use the following code to convert the $person_json JSON string obtained in the previous step into an array:

$person_array = json_decode($person_json, true);

The above code converts the $person_json JSON string into an array and store the result in the $person_array variable.

It should be noted that when the second parameter is true, json_decode() returns an associative array instead of an object. Therefore, the value of the $person_array variable is:

[
    'name' => '张三',
    'age' => 21,
]

Converting a JSON string to an object

is similar to converting a JSON string to an array, we can also Use the json_decode() function to convert a JSON string into an object. For example, we can use the following code to convert the $person_json JSON string into a Person object:

$person_obj = json_decode($person_json);

The above code will convert the $person_json JSON The string is converted to a Person object and the result is stored in the $person_obj variable.

It should be noted that when the second parameter is false or omitted, json_decode() returns an object without a defined class. Therefore, by default the type of the $person_obj variable is stdClass, and the properties of the object can be accessed as follows:

$person_name = $person_obj->name;
$person_age = $person_obj->age;

Convert the array to a JSON string

In PHP, we can also use the json_encode() function to convert an array into a JSON string. For example, we can use the following code to convert the $person_array array to a JSON string:

$person_array_json = json_encode($person_array);

The above code converts the $person_array array to a JSON string and The results are stored in the $person_array_json variable. Similar to the previous step, the value of $person_array_json is:

{"name":"张三","age":21}

Convert the array to an object array

In PHP, we can use json_decode() Function converts a JSON string into an array. If this array contains multiple elements, we can use foreach to iterate through them and convert them to objects.

For example, we can use the following JSON string to represent multiple Person objects:

[
    {"name":"张三","age":21},
    {"name":"李四","age":22},
    {"name":"王五","age":23}
]

To convert this JSON string into an object array, we can use the following Code:

$persons_array = json_decode($persons_json);

$persons = [];
foreach ($persons_array as $person_data) {
    $person = new Person();
    $person->name = $person_data->name;
    $person->age = $person_data->age;
    $persons[] = $person;
}

The above code first uses the json_decode() function to convert the $persons_json JSON string into an array. Next, we define an empty array $persons and use foreach to loop through the elements in $persons_array, that is, each Person object The data. For each $person_data, we create a new Person object and set its property values ​​to the corresponding values ​​in $person_data. Afterwards, we add the object to the $persons array, resulting in an array containing multiple Person objects.

Conclusion

This article introduces how to get objects in PHP and convert them into different data types such as JSON strings, arrays, and object arrays. These operations are often used in web development and I hope they will be helpful to readers.

The above is the detailed content of How to get objects and convert data types 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