In modern web development, JavaScript-based front-end programs using JSON (JavaScript Object Notation) format data have become a regular choice. PHP is a common server-side programming language. Often we need to convert PHP objects or arrays into JSON format strings on the server side to facilitate front-end calls. In this article, we will show you how to convert an array of objects into a JSON string array in PHP.
The json_encode() function is built into PHP, which can convert PHP arrays and objects into JSON format strings, as follows Display:
<?php $array = array('name' => '张三', 'age' => 18, 'gender' => 'male'); echo json_encode($array); ?>
Run result:
{"name":"张三","age":18,"gender":"male"}
In this example we can see that when the PHP array is passed into the json_encode() function, an encoded JSON format string can be returned. Note here: the json_encode() function only supports UTF-8 encoding format. If the encoding format used by your string is not UTF-8, an error will occur. Therefore, before using the json_encode() function, please ensure that the array or object that needs to be converted has been converted to UTF-8 format.
If you need to convert the object array into a JSON string array, you need to convert the object into an array and use the json_encode() function for processing. For example, we need to convert the object array representing student information into a JSON format string array:
<?php class Student{ public $name; public $age; public $gender; } $student1 = new Student(); $student1->name = '张三'; $student1->age = 18; $student1->gender = 'male'; $student2 = new Student(); $student2->name = '李四'; $student2->age = 19; $student2->gender = 'female'; $students = array(); array_push($students,$student1,$student2); $students_arr = array(); foreach($students as $student){ $obj = get_object_vars($student); array_push($students_arr,$obj); } echo json_encode($students_arr); ?>
Running result:
[{"name":"张三","age":18,"gender":"male"},{"name":"李四","age":19,"gender":"female"}]
In the above example, we created an array that can represent Student class for student information, and uses an array to store multiple Student objects. When converting the Student object to an array, the get_object_vars() function is used. This function converts the object into an associative array, allowing us to handle the object like an array. Finally, pass the array to the json_encode() function and output the result.
At the same time, in order to better assist understanding, we can also transform the above example into a more intuitive form, as shown in the following figure:
Through the above two examples, you have learned how to convert a PHP object or array into a JSON format string array. However, at first glance these JSON formatted string arrays look a bit hard to read. In order to beautify the JSON data format, you can use the second parameter of the json_encode() function: JSON_PRETTY_PRINT selection, as shown below:
<?php $array = array('name' => '张三', 'age' => 18, 'gender' => 'male'); echo json_encode($array,JSON_PRETTY_PRINT); ?>
Running result:
{ "name": "张三", "age": 18, "gender": "male" }
This is what happens after using the JSON_PRETTY_PRINT option The output results are clearer and easier to read.
In this article, we introduced how to convert an array of objects into a JSON string array in PHP. This is an important step in our front-end and back-end interaction. Combined with the front-end JSON parsing tool, the data can be easily converted into the format required by each website.
The above is the detailed content of How to convert php object array into json string array. For more information, please follow other related articles on the PHP Chinese website!