Home Backend Development PHP Problem How to convert variables into arrays in php

How to convert variables into arrays in php

Apr 17, 2023 pm 02:12 PM

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);
Copy after login

Output result:

Array
(
    [0] => apple
    [1] => banana
    [2] => orange
)
Copy after login

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);
Copy after login

Output result:

Array
(
    [0] => H
    [1] => e
    [2] => l
    [3] => l
    [4] => o
    [5] => ,
    [6] =>  
    [7] => P
    [8] => H
    [9] => P
    [10] => !
)
Copy after login

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);
Copy after login

Output result:

Array
(
    [0] => 123
)
Array
(
    [0] => 1
)
Array
(
)
Copy after login

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);
Copy after login

Output result:

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

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);
Copy after login

Output results:

Array
(
    [name] => Tom
    [age] => 18
    [address] => Array
        (
            [country] => China
            [city] => Beijing
        )
)
Copy after login

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!

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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What are the best practices for deduplication of PHP arrays What are the best practices for deduplication of PHP arrays Mar 03, 2025 pm 04:41 PM

What are the best practices for deduplication of PHP arrays

What Are the Latest PHP Coding Standards and Best Practices? What Are the Latest PHP Coding Standards and Best Practices? Mar 10, 2025 pm 06:16 PM

What Are the Latest PHP Coding Standards and Best Practices?

How Do I Work with PHP Extensions and PECL? How Do I Work with PHP Extensions and PECL? Mar 10, 2025 pm 06:12 PM

How Do I Work with PHP Extensions and PECL?

Can PHP array deduplication take advantage of key name uniqueness? Can PHP array deduplication take advantage of key name uniqueness? Mar 03, 2025 pm 04:51 PM

Can PHP array deduplication take advantage of key name uniqueness?

How to Implement message queues (RabbitMQ, Redis) in PHP? How to Implement message queues (RabbitMQ, Redis) in PHP? Mar 10, 2025 pm 06:15 PM

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

Does PHP array deduplication need to be considered for performance losses? Does PHP array deduplication need to be considered for performance losses? Mar 03, 2025 pm 04:47 PM

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

What are the optimization techniques for deduplication of PHP arrays What are the optimization techniques for deduplication of PHP arrays Mar 03, 2025 pm 04:50 PM

What are the optimization techniques for deduplication of PHP arrays

How to Use Reflection to Analyze and Manipulate PHP Code? How to Use Reflection to Analyze and Manipulate PHP Code? Mar 10, 2025 pm 06:12 PM

How to Use Reflection to Analyze and Manipulate PHP Code?

See all articles