Home >Backend Development >PHP Problem >How to convert data into array in php? Introduction to built-in functions

How to convert data into array in php? Introduction to built-in functions

PHPz
PHPzOriginal
2023-04-19 10:08:39463browse

In PHP development, arrays are one of the most important data types. In many cases, we need to save some data in the form of an array in memory. In order to easily manipulate this data, we need to convert them into PHP arrays.

In PHP, many built-in functions are provided to achieve this purpose. Next, we will introduce some commonly used PHP functions for converting different data types into PHP arrays.

  1. Convert a string to an array

In PHP, you can use the explode() function to split a string into an array. This function requires two parameters: the first is the delimiter, and the second is the string to be split. The return value of this function is an array containing the split string.

The following is the sample code:

$str = 'Hello,world,how,are,you';
$arr = explode(',', $str);
print_r($arr);

Output result:

Array
(
    [0] => Hello
    [1] => world
    [2] => how
    [3] => are
    [4] => you
)
  1. Convert object to array

In PHP, you can Use the get_object_vars() function to convert an object into an associative array. This function requires one parameter, which is the object to be converted. The return value of this function is an associative array containing the object's properties and values.

The following is the sample code:

class Person {
    public $name;
    public $age;
    public $sex;
    function __construct($name, $age, $sex) {
        $this->name = $name;
        $this->age = $age;
        $this->sex = $sex;
    }
}
$person = new Person('Tom', 20, 'male');
$arr = get_object_vars($person);
print_r($arr);

Output result:

Array
(
    [name] => Tom
    [age] => 20
    [sex] => male
)
  1. Convert JSON string to array

In PHP , you can use the json_decode() function to convert a JSON string into a PHP array. This function takes two parameters: the first is the JSON string to be converted, and the second is a Boolean value indicating whether it will be converted to an associative array. The return value of this function is the converted PHP array.

The following is the sample code:

$json_str = '{"name":"Tom", "age":20, "sex":"male"}';
$arr = json_decode($json_str, true);
print_r($arr);

Output result:

Array
(
    [name] => Tom
    [age] => 20
    [sex] => male
)
  1. Convert XML string to array

In PHP , you can use the SimpleXMLElement class to convert an XML string into a PHP array. This class provides a method asArray() to implement this function. This method does not need to pass in parameters and can convert the object instance into a PHP array.

The following is the sample code:

$xml_str = '<person><name>Tom</name><age>20</age><sex>male</sex></person>';
$xml = new SimpleXMLElement($xml_str);
$arr = $xml->asArray();
print_r($arr);

Output result:

Array
(
    [person] => Array
        (
            [name] => Tom
            [age] => 20
            [sex] => male
        )

)

Summary

The above is a method to convert some common data types into PHP arrays. In the actual development process, we need to use these functions flexibly to convert data into the form we need to facilitate operation and management. Of course, it should be noted that data type conversion not only needs to consider the compatibility of the data format, but also the accuracy and security of the data.

The above is the detailed content of How to convert data into array in php? Introduction to built-in functions. 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