How to convert variables into arrays in php

PHPz
Release: 2023-04-17 14:49:57
Original
745 people have browsed it

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!

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