Home > Backend Development > PHP Problem > How to convert multiple object arrays to JSON object array in php

How to convert multiple object arrays to JSON object array in php

PHPz
Release: 2023-04-23 16:14:33
Original
568 people have browsed it

In PHP development, we often need to convert string arrays into JSON objects, especially when interacting with the front end. But sometimes we encounter situations where we need to convert multiple object arrays into JSON object arrays. This article will detail how to convert multiple object arrays into JSON object arrays.

1. Convert a string into an object array

In PHP, we can use the json_decode() function to convert a JSON-formatted string into an object array. The sample code is as follows:

$jsonStr = '{"name": "Tom", "age": 20}';
$jsonArr = json_decode($jsonStr, true);
var_dump($jsonArr);
Copy after login

The output is as follows:

array(2) {
    ["name"]=>
    string(3) "Tom"
    ["age"]=>
    int(20)
}
Copy after login

When using the json_decode() function, when the second parameter is true, it means converting the JSON format string into an associative array, otherwise it will Convert to object.

2. Convert the object array into a JSON object array

First of all, we need to clarify the format of the JSON object array. It is usually an array composed of multiple JSON objects. Each JSON object Contains several key-value pairs. An example is as follows:

[
    {"name": "Tom", "age": 20},
    {"name": "Jerry", "age": 18},
    {"name": "Emma", "age": 25}
]
Copy after login

So, how to convert multiple object arrays in PHP into the above JSON object array? We can do this using a loop.

The sample code is as follows:

//对象数组1
$objArr1 = [
    ["name" => "Tom", "age" => 20],
    ["name" => "Jerry", "age" => 18]
];
//对象数组2
$objArr2 = [
    ["name" => "Emma", "age" => 25],
    ["name" => "Lily", "age" => 22]
];

//JSON对象数组
$jsonArr = [];
foreach ($objArr1 as $item) {
    $jsonArr[] = json_decode(json_encode($item));
}
foreach ($objArr2 as $item) {
    $jsonArr[] = json_decode(json_encode($item));
}

//输出JSON对象数组
echo json_encode($jsonArr);
Copy after login

Explain the above code. First, two PHP object arrays $objArr1 and $objArr2 are defined. Then define an empty JSON object array $jsonArr, and use a loop to convert each object in $objArr1 and $objArr2 into a JSON object and add it to $jsonArr. Finally, use the json_encode() function to convert $jsonArr into a string output in JSON format, thereby obtaining the required JSON object array.

3. Convert multiple object arrays into object array arrays

In some cases, we need to convert multiple object arrays into one object array array instead of one merged Array of JSON objects. At this point, we just need to convert each object array into an object array in the loop and add them to an array.

The sample code is as follows:

//对象数组1
$objArr1 = [
    ["name" => "Tom", "age" => 20],
    ["name" => "Jerry", "age" => 18]
];
//对象数组2
$objArr2 = [
    ["name" => "Emma", "age" => 25],
    ["name" => "Lily", "age" => 22]
];

//对象数组数组
$objArrArr = [];
foreach ($objArr1 as $item) {
    $objArrArr[] = json_decode(json_encode($item));
}
foreach ($objArr2 as $item) {
    $objArrArr[] = json_decode(json_encode($item));
}

//输出对象数组数组
var_dump($objArrArr);
Copy after login

The output is as follows:

array(4) {
    [0]=>
    object(stdClass)#1 (2) {
        ["name"]=>
        string(3) "Tom"
        ["age"]=>
        int(20)
    }
    [1]=>
    object(stdClass)#2 (2) {
        ["name"]=>
        string(5) "Jerry"
        ["age"]=>
        int(18)
    }
    [2]=>
    object(stdClass)#3 (2) {
        ["name"]=>
        string(4) "Emma"
        ["age"]=>
        int(25)
    }
    [3]=>
    object(stdClass)#4 (2) {
        ["name"]=>
        string(4) "Lily"
        ["age"]=>
        int(22)
    }
}
Copy after login

In the above code, we define two object arrays $objArr1 and $objArr2, and define an empty Object array array $objArrArr. Then loop, convert each object in $objArr1 and $objArr2 into an object array, and add them to $objArrArr. Finally, output $objArrArr to get the required object array array.

Summary

This article mainly introduces how to convert multiple object arrays into JSON object arrays and object array arrays. When performing JSON conversion, you need to pay attention to the second parameter in the json_decode() function. Usually we recommend using true to convert the JSON format string into an associative array. When using a loop conversion, you need to pay attention to converting each object into a JSON object or an array of objects and adding them to an array.

The above is the detailed content of How to convert multiple object arrays to JSON object array in php. 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