Home  >  Article  >  Backend Development  >  How to convert array to object array in php

How to convert array to object array in php

PHPz
PHPzOriginal
2023-04-12 09:14:16673browse

In PHP, arrays and objects are two different forms, but we often need to convert between them. This article will explain how to convert an array to an array of objects and convert an array of objects back to a normal array in PHP.

First, let's take a look at how to convert a normal array to an object array. In PHP we can achieve this using casting (casting an array to an object) or manually creating an object. The following is a demonstration of the two methods:

Method 1: Forced type conversion

$arr = array('id'=>1, 'name'=>'John', 'age'=>25);
$obj = (object)$arr;
print_r($obj);

Output:

stdClass Object
(
    [id] => 1
    [name] => John
    [age] => 25
)

In the above code, we used (object )operator converts an array into an object. This operator is actually a cast that converts a scalar value, array, or resource into an object. Now $obj is an object and we can access its properties just like a normal object.

Method 2: Manually create the object

$arr = array('id'=>1, 'name'=>'John', 'age'=>25);
$obj = new stdClass();
foreach ($arr as $key=>$value) {
   $obj->$key = $value;
}
print_r($obj);

Output:

stdClass Object
(
    [id] => 1
    [name] => John
    [age] => 25
)

In the above code, we manually created an empty object$obj , and then use a loop to traverse the array and assign each key-value pair of the array as a property of the object. Now $obj is an object with three properties.

Next, let’s take a look at how to convert an array of objects into a normal array. In PHP we can achieve this using casting (casting an object to an array) or manually iterating over an array of objects. The following is a demonstration of the two methods:

Method 1: Forced type conversion

$obj1 = new stdClass();
$obj1->id = 1;
$obj1->name = 'John';
$obj1->age = 25;

$obj2 = new stdClass();
$obj2->id = 2;
$obj2->name = 'Jane';
$obj2->age = 30;

$arr = array($obj1, $obj2);
$arr = (array)$arr;
print_r($arr);

Output:

Array
(
    [0] => stdClass Object
        (
            [id] => 1
            [name] => John
            [age] => 25
        )

    [1] => stdClass Object
        (
            [id] => 2
            [name] => Jane
            [age] => 30
        )
)

In the above code, we used (array )operator converts an array of objects into a normal array. This operator is actually a cast that converts an object into an array. Now $arr is a normal array and we can access its elements just like a normal array.

Method 2: Manually traverse the object array

$obj1 = new stdClass();
$obj1->id = 1;
$obj1->name = 'John';
$obj1->age = 25;

$obj2 = new stdClass();
$obj2->id = 2;
$obj2->name = 'Jane';
$obj2->age = 30;

$arr = array($obj1, $obj2);
$new_arr = array();
foreach ($arr as $obj) {
   $new_arr[] = (array)$obj;
}
print_r($new_arr);

Output:

Array
(
    [0] => Array
        (
            [id] => 1
            [name] => John
            [age] => 25
        )

    [1] => Array
        (
            [id] => 2
            [name] => Jane
            [age] => 30
        )

)

In the above code, we traverse the object array $arr, and Each object is cast to an array and then added to a new array $new_arr. Now $new_arr is a normal array with two elements, each element is an associative array containing three key-value pairs.

Summary

In PHP, we can convert between arrays and objects using casts or manual traversal. No matter which method you choose, as long as you understand the difference between arrays and objects, you can easily convert. In actual development, this ability is very common, because sometimes we need to convert arrays into objects in order to better operate on them.

The above is the detailed content of How to convert array to object array 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