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

How to convert object to array in PHP

PHPz
PHPzOriginal
2023-04-25 09:19:19401browse

In PHP programming, array is a very commonly used data type. It can store a set of related data and facilitate addition, deletion, modification and query operations on these data. Sometimes, we need to convert an object into an array for operation. This article will introduce how to convert an object into an array in PHP.

In PHP, an object is a complex data type that is an instance instantiated from a class. Each object has its own properties and methods. There are two methods for converting objects into arrays in PHP: specified property names and iterators.

1. Use the specified attribute name

If we only need to convert the object's attribute name and value into an array, we can use the object's get_object_vars() method. This method returns an associative array with property names as keys and property values ​​as values. The following is a simple example:

class Person {
  public $name;
  public $age;
  function __construct($name, $age) {
    $this->name = $name;
    $this->age = $age;
  }
}
 
$p = new Person("John", 25);
$arr = get_object_vars($p);
print_r($arr);

The output result is:

Array
(
    [name] => John
    [age] => 25
)

From the above example we can see that using the get_object_vars() method only requires passing the object as a parameter to the function. Can. The function returns an array of property names and values.

Sometimes, we need to exclude certain attributes to avoid appearing in the array. In this case, you can use the object's __sleep() method. This method returns an array containing the property names, excluding unnecessary properties. The following is an example:

class Person {
  public $name;
  public $age;
  public $id;
  function __construct($name, $age, $id) {
    $this->name = $name;
    $this->age = $age;
    $this->id = $id;
  }
  public function __sleep() {
    return array('name', 'age');
  }
}
 
$p = new Person("John", 25, "123456");
$arr = get_object_vars($p);
print_r($arr);

The output result is:

Array
(
    [name] => John
    [age] => 25
)

From the above example, we can see that the __sleep() method returns an array composed of unnecessary attributes. When converting an object to an array, only these properties are retained.

2. Use iterators

If we need a more flexible way to convert objects into arrays, we can use iterators. An iterator is a class that implements the Iterator interface. This interface specifies the methods required to convert an object into an array.

The process of using an iterator to convert an object into an array is as follows:

  • Create a class that implements the Iterator interface
  • Returns an array containing the object to be converted Attribute name and value
  • Call the iterator_to_array() function to convert the iterator into an array

The following is an example:

class Person implements Iterator {
  private $position = 0;
  private $data = array(
    'name' => 'John',
    'age' => 25,
    'id' => '123456'
  );
 
  public function current() {
    $key = array_keys($this->data)[$this->position];
    return array($key => $this->data[$key]);
  }
 
  public function key() {
    return $this->position;
  }
 
  public function next() {
    ++$this->position;
  }
 
  public function rewind() {
    $this->position = 0;
  }
 
  public function valid() {
    $keys = array_keys($this->data);
    return isset($keys[$this->position]);
  }
}
 
$p = new Person;
$arr = iterator_to_array($p);

print_r($arr);

The output result is:

Array
(
    [0] => Array
        (
            [name] => John
        )

    [1] => Array
        (
            [age] => 25
        )

    [2] => Array
        (
            [id] => 123456
        )

)

We can see from the above example that when the iterator_to_array() function is called to convert the iterator into an array, the returned array will consist of all attribute names and values.

Summary: Converting objects to arrays is very convenient in PHP. We can use the get_object_vars() method or iterator to implement conversion. No matter which method you use, you can convert the object into a data type that is convenient for manipulation.

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