php convert array to array

WBOY
Release: 2023-05-19 21:25:38
Original
553 people have browsed it

In PHP, sometimes we need to convert arrays into objects in order to manipulate data more conveniently. This process is called array to object. Vice versa, there are times when we need to convert objects to arrays for better processing of data. This process is called object to array. Below we will focus on how to convert arrays to objects and objects to arrays in PHP.

  1. Convert an array to an object

To convert an array to an object you can use the built-in function arrayToObject in PHP. The implementation of this function is relatively simple. Its principle is to implement an empty object through PHP's built-in class stdClass, and use the elements of the original array as attributes of the new object. The following is an example of using this function:

function arrayToObject($arr) {
    if (is_array($arr)) {
        $obj = new stdClass();
 
        foreach ($arr as $key => $val){
            $obj->$key = $val;
        }
    }
    else {
        $obj = $arr;
    }
 
    return $obj;
}
Copy after login

An example of using this function to convert an array into an object is as follows:

$arr = array(
    'name' => 'Tom',
    'age' => 20,
    'gender' => 'male'
);
 
$obj = arrayToObject($arr);
 
var_dump($obj); // 输出结果:object(stdClass)#1 (3) { ["name"]=> string(3) "Tom" ["age"]=> int(20) ["gender"]=> string(4) "male" }
Copy after login

As you can see from the output above, we successfully converted the array For objects. This makes it easy to use the properties of the object to get the corresponding value.

  1. Convert Object to Array

Similarly, converting an object to an array can also be done using built-in functions in PHP. The specific implementation is to traverse the properties of the object and convert its key-value pairs into array elements. The following is an example of using this function:

function objectToArray($obj) {
    if (is_object($obj)) {
        $obj = get_object_vars($obj);
    }
 
    if (is_array($obj)) {
        return array_map(__FUNCTION__, $obj);
    }
    else {
        return $obj;
    }
}
Copy after login

An example of using this function to convert an object into an array is as follows:

$obj = new stdClass();
$obj->name = 'Tom';
$obj->age = 20;
$obj->gender = 'male';

$arr = objectToArray($obj);

var_dump($arr); // 输出结果:array(3) { ["name"]=> string(3) "Tom" ["age"]=> int(20) ["gender"]=> string(4) "male" }
Copy after login

As you can see from the output, we have successfully converted the object to array. This makes it easy to use arrays to get data.

Summary

In PHP, converting arrays to objects and objects to arrays are very common operations. Through the explanation of this article, we learned how to use PHP's built-in functions to achieve these functions. For developers, mastering these skills can not only improve development efficiency, but also better process data and make a huge contribution to application optimization.

The above is the detailed content of php convert array to 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!