How to convert variables into arrays in php
Apr 17, 2023 pm 02:12 PMIn PHP, variables can be converted into arrays to make data processing more convenient and efficient. Below we will explain in detail how to convert variables of different types into arrays.
1. Convert a string into an array
1.1 Use the explode function
The explode function can split a string into an array according to the specified delimiter. The sample code is as follows:
$str = "apple,banana,orange"; $arr = explode(",", $str); print_r($arr);
Output result:
Array ( [0] => apple [1] => banana [2] => orange )
1.2 Use str_split function
str_split function can split a string into an array of single characters. The sample code is as follows:
$str = "Hello, PHP!"; $arr = str_split($str); print_r($arr);
Output result:
Array ( [0] => H [1] => e [2] => l [3] => l [4] => o [5] => , [6] => [7] => P [8] => H [9] => P [10] => ! )
2. Convert numbers, Boolean values or null into arrays
2.1 Use conversion characters
You can use conversion characters to convert numbers, Boolean values or Convert null into an array. The sample code is as follows:
$num = 123; $arr = (array)$num; print_r($arr); $bool = true; $arr = (array)$bool; print_r($arr); $null = null; $arr = (array)$null; print_r($arr);
Output result:
Array ( [0] => 123 ) Array ( [0] => 1 ) Array ( )
3. Convert the object into an array
3.1 Use the get_object_vars function
get_object_vars The function can convert the object into an associative array, where the key name is the object attribute name. The sample code is as follows:
class Person { public $name; public $age; public function __construct($name, $age) { $this->name = $name; $this->age = $age; } } $person = new Person("Tom", 18); $arr = get_object_vars($person); print_r($arr);
Output result:
Array ( [name] => Tom [age] => 18 )
3.2 Nested use conversion
If Object attribute values are also objects and can be converted into arrays using recursive calls. The sample code is as follows:
class Person { public $name; public $age; public $address; public function __construct($name, $age, $address) { $this->name = $name; $this->age = $age; $this->address = $address; } } class Address { public $country; public $city; public function __construct($country, $city) { $this->country = $country; $this->city = $city; } } $address = new Address("China", "Beijing"); $person = new Person("Tom", 18, $address); $arr = (array)$person; if (is_object($person->address)) { $arr["address"] = (array)$person->address; } print_r($arr);
Output results:
Array ( [name] => Tom [age] => 18 [address] => Array ( [country] => China [city] => Beijing ) )
The above is the method for converting variables into arrays. I hope it can be useful for You helped!
The above is the detailed content of How to convert variables into arrays in php. For more information, please follow other related articles on the PHP Chinese website!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

What are the best practices for deduplication of PHP arrays

What Are the Latest PHP Coding Standards and Best Practices?

How Do I Work with PHP Extensions and PECL?

Can PHP array deduplication take advantage of key name uniqueness?

How to Implement message queues (RabbitMQ, Redis) in PHP?

Does PHP array deduplication need to be considered for performance losses?

What are the optimization techniques for deduplication of PHP arrays

How to Use Reflection to Analyze and Manipulate PHP Code?
