Home > Backend Development > PHP Problem > How to convert php object array into json string array

How to convert php object array into json string array

PHPz
Release: 2023-04-19 15:05:56
Original
458 people have browsed it

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.

  1. Use json_encode() function

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(&#39;name&#39; => '张三', 'age' => 18, 'gender' => 'male');
echo json_encode($array);
?>
Copy after login

Run result:

{"name":"张三","age":18,"gender":"male"}
Copy after login

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.

  1. JSON formatting of object array

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);
?>
Copy after login

Running result:

[{"name":"张三","age":18,"gender":"male"},{"name":"李四","age":19,"gender":"female"}]
Copy after login

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:

  1. JSON_PRETTY_PRINT option

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(&#39;name&#39; => '张三', 'age' => 18, 'gender' => 'male');
echo json_encode($array,JSON_PRETTY_PRINT);
?>
Copy after login

Running result:

{
    "name": "张三",
    "age": 18,
    "gender": "male"
}
Copy after login

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!

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