Home  >  Article  >  Backend Development  >  A brief analysis of several methods of converting irregular objects into arrays

A brief analysis of several methods of converting irregular objects into arrays

PHPz
PHPzOriginal
2023-04-23 10:21:02412browse

In PHP, we sometimes need to convert an object into an array to facilitate our data operations. Normally, if the object's attribute name is the same as the array's key value, we can directly use the object-to-array function, for example:

$array = (array) $obj; // $obj为对象,$array为数组

However, if the object's attribute name is irregular, for example, in the attribute name With underscores, hyphens, or capital letters, etc., at this time we need to manually process the attribute names and convert them into legal array key names. Below we introduce several methods of converting irregular objects into arrays.

Method 1: Use the get_object_vars() function

The get_object_vars() function can be used to obtain all attributes and attribute values ​​of the object and return an associative array. We can use this function to obtain the object attribute list and then process it.

$obj = new stdClass();
$obj->user_name = '张三';
$obj->user_age = 20;
$obj->{'user_city-name'} = '北京';

$arr = array();
$vars = get_object_vars($obj);
foreach($vars as $key => $value){
    $arr[str_replace('_', '-', $key)] = $value;
}

var_export($arr);

In the above example, we use the get_object_vars() function to obtain all the attributes and attribute values ​​of the object, and then use a foreach loop to traverse and convert the underscores in the attribute names to hyphens. The final converted array is as follows:

array (
  'user_name' => '张三',
  'user_age' => 20,
  'user-city-name' => '北京',
)

Note that this method only processes the properties of the object and does not include inherited properties.

Method 2: Use the json_encode() and json_decode() functions

We can also use the json_encode() and json_decode() functions to convert the object into JSON format, and then Use the json_decode() function to convert the JSON format back to an array.

$obj = new stdClass();
$obj->user_name = '张三';
$obj->user_age = 20;
$obj->{'user_city-name'} = '北京';

$json_str = json_encode($obj);
$arr = json_decode($json_str, true);

foreach($arr as $key => $value){
    $new_key = str_replace('_', '-', $key);
    unset($arr[$key]);
    $arr[$new_key] = $value;
}

var_export($arr);

In the above example, we use the json_encode() function to convert the object into JSON format, and then use the json_decode() function to convert the JSON format back to an array. Then use a foreach loop to traverse the array and convert the underscores in the attribute names into hyphens. The final converted array is the same as method one.

Method 3: Use the array_map() function

The array_map() function can apply a callback function to each element in the array and return a new array. We can use this function to convert underscores in property names to hyphens.

$obj = new stdClass();
$obj->user_name = '张三';
$obj->user_age = 20;
$obj->{'user_city-name'} = '北京';

$arr = (array) $obj;
$arr = array_map(function($key){
    return str_replace('_', '-', $key);
}, array_keys($arr));
$arr = array_combine($arr, (array) $obj);

var_export($arr);

In the above example, we use the array_map function to convert the underscore in the attribute name into a hyphen, use the array_keys() function to obtain the attribute list of the object, and then use the array_combine() function to convert the attribute name and attribute value Convert to array. The final converted array is the same as method one and method two.

Summary:

When we need to convert an object into an array and encounter irregular attribute names, we can use get_object_vars(), json_encode(), array_map() and other functions to process it. Through these methods, we can easily convert irregular objects into regular arrays to facilitate our data operations.

The above is the detailed content of A brief analysis of several methods of converting irregular objects into arrays. 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