How to convert php object into string array object

PHPz
Release: 2023-04-26 14:49:10
Original
444 people have browsed it

In PHP development, we often need to convert an object into a string array object in order to pass it to other programs or save it to the database. Although in PHP, objects can be implemented through JSON serialization or array conversion, the results of these conversions do not fully meet the requirements because they do not retain all attribute information of the object. In this article, we will introduce how to convert PHP objects into complete string array objects to meet practical needs.

1. Basic introduction to PHP objects

In PHP, an object is a special data type that can be created from a class definition. When creating an object, we can define some properties and methods for it to facilitate access and manipulation of the object's data.

For example, here is a sample class definition:

class Person {
    public $name;
    public $age;
    
    public function sayHello() {
        echo 'Hello, my name is ' . $this->name . ', and I am ' . $this->age . ' years old.';
    }
}
Copy after login

In the above example, we define a Person class that contains two attributes ($name and $age) and A method (sayHello). We can create a Person object in the following ways:

$person = new Person();
$person->name = 'Tom';
$person->age = 20;
Copy after login

After creating the object, we can access and operate its properties and methods:

echo $person->name . ' is ' . $person->age . ' years old.';
$person->sayHello();
Copy after login

2. Convert PHP objects into arrays

In PHP, we can convert objects into arrays in the following two ways:

  1. Forced type conversion method

This method uses the forced type conversion symbol (array) Convert the object into an array, for example:

$personArr = (array) $person;
print_r($personArr);
Copy after login

The converted array result is as follows:

Array
(
    [name] => Tom
    [age] => 20
)
Copy after login

In the above example, we first cast the $person object, and then Print it out. The converted result only contains the public properties of the object and does not contain the object's methods.

  1. Object to Array Method

This method uses the PHP built-in function get_object_vars() and the class definition __toString() method to convert the object into an array. For example:

class Person {
    public $name;
    public $age;
    
    public function __toString() {
        return 'Name: ' . $this->name . ', Age: ' . $this->age;
    }
}

$person = new Person();
$person->name = 'Tom';
$person->age = 20;

$personArr = get_object_vars($person);
echo $person . "\n";
print_r($personArr);
Copy after login

The converted result is as follows:

Name: Tom, Age: 20
Array
(
    [name] => Tom
    [age] => 20
)
Copy after login

In the above example, we defined the Person class and implemented the __toString() method in the class definition, which returns a string. After we create the object, we first convert the $person object into a string, and then use the get_object_vars() function to convert the object into an array. The converted result contains all public properties of the object.

3. Convert PHP object into string array object

For a more complex object, we may need to convert it into a complete string array object, including the object's attributes and method.

The following is a specific implementation of converting a PHP object into a string array object:

function object_to_array($obj) {
    if (is_object($obj)) {
        // 获取该对象的所有公共属性
        $objArr = get_object_vars($obj);
        // 获取该对象所有方法
        $objMethods = get_class_methods(get_class($obj));
 
        // 枚举所有属性,为每个属性创建一个关联数组项
        $arr = array();
        foreach ($objArr as $key => $value) {
            // 如果该属性是一个对象,递归处理
            if (is_object($value)) {
                $arr[$key] = object_to_array($value);
            } else {
                $arr[$key] = $value;
            }
        }
 
        // 枚举所有方法,为每个方法创建一个关联数组项
        foreach ($objMethods as $value) {
            $arr[$value] = true;
        }
 
        return $arr;
    }
    return $obj;
}

class Person {
    public $name;
    public $age;
    
    public function sayHello() {
        echo 'Hello, my name is ' . $this->name . ', and I am ' . $this->age . ' years old.';
    }
}

$person = new Person();
$person->name = 'Tom';
$person->age = 20;

$personArr = object_to_array($person);

print_r($personArr);
Copy after login

In the above example, we define an object_to_array() function, which receives an object as parameter. The function first gets all the public properties and methods of the object and saves them into two arrays. The function then enumerates all properties and, if the property is an object, processes the object recursively. Finally, the function returns an associative array with properties and methods as keys.

If we run the above example, the output will be as follows:

Array
(
    [name] => Tom
    [age] => 20
    [sayHello] => 1
)
Copy after login

In the above example, we created an array named $personArr, which contains $person All properties and methods of the object. Therefore, we successfully converted the PHP object into a complete string array object.

Conclusion

This article introduces how to convert PHP objects into string array objects. We first understood the basic concepts and usage of PHP objects, then discussed two methods of converting PHP objects to arrays, and finally implemented a method of converting PHP objects into complete string array objects. This method can be used when saving object data to the database or transferring object data to other programs to meet actual needs.

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