search
HomeBackend DevelopmentPHP ProblemHow to convert php object array into json string array

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);
?>

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.

  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);
?>

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:

  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);
?>

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)