How to convert variables into arrays in php
In 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 AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

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



PHP 8's JIT compilation enhances performance by compiling frequently executed code into machine code, benefiting applications with heavy computations and reducing execution times.

The article discusses OWASP Top 10 vulnerabilities in PHP and mitigation strategies. Key issues include injection, broken authentication, and XSS, with recommended tools for monitoring and securing PHP applications.

The article discusses securing PHP file uploads to prevent vulnerabilities like code injection. It focuses on file type validation, secure storage, and error handling to enhance application security.

The article discusses symmetric and asymmetric encryption in PHP, comparing their suitability, performance, and security differences. Symmetric encryption is faster and suited for bulk data, while asymmetric is used for secure key exchange.

The article discusses implementing robust authentication and authorization in PHP to prevent unauthorized access, detailing best practices and recommending security-enhancing tools.

Article discusses retrieving data from databases using PHP, covering steps, security measures, optimization techniques, and common errors with solutions.Character count: 159

The article discusses strategies to prevent CSRF attacks in PHP, including using CSRF tokens, Same-Site cookies, and proper session management.

Prepared statements in PHP enhance database security and efficiency by preventing SQL injection and improving query performance through compilation and reuse.Character count: 159
