```In the above code, we first define a variable `$value`, whose value is the string `"example"`. Then we use `(a"/> ```In the above code, we first define a variable `$value`, whose value is the string `"example"`. Then we use `(a">

Home  >  Article  >  Backend Development  >  How to convert variables into arrays in php

How to convert variables into arrays in php

PHPz
PHPzOriginal
2023-04-12 09:21:32612browse

In PHP, you can convert a variable into an array and add the variable value to the array. The following is the sample code:

<?php
// 定义一个变量
$value = "example";

// 把变量转为数组
$arr = (array) $value;

// 打印数组
print_r($arr);
?>

In the above code, we first define a variable $value, whose value is the string "example". Then we use the (array) operator to convert it to an array, and the variable $arr becomes an array, which contains the value of the variable $value.

You can see that when we convert the variable $value into an array, the array contains only one element, its key name is 0, and the key value is a variable The value of $value.

Use this method to convert variables into arrays, and you can easily add the value of a variable to the array. For example, we can define an empty array, then convert multiple variables into arrays and add them to this array:

<?php
// 定义变量
$name = "Tom";
$age = 25;
$city = "New York";

// 定义空数组
$info = array();

// 把变量转为数组并添加到数组中
$info[] = (array) $name;
$info[] = (array) $age;
$info[] = (array) $city;

// 打印数组
print_r($info);
?>

In the above code, we define three variables$name, $age and $city, which represent the person's name, age and city respectively. Then, we define an empty array $info to store the values ​​of these variables.

Next, we use the (array) operator to convert the variables $name, $age and $city is an array and adds them to the array $info, using $info[] means adding elements to the end of the array. Finally, we use the print_r() function to print the elements in the array $info.

It should be noted that when converting a variable to an array, if the value of the variable is an object, the public member variables of the object will be used as the key value, and the name of the member variable will be used as the key name. For example:

<?php
// 定义一个对象
class Person {
    public $name = "Tom";
    public $age = 25;
}

// 定义一个变量,值为对象
$person = new Person();

// 把变量转为数组
$arr = (array) $person;

// 打印数组
print_r($arr);
?>

In the above code, we first define a class Person, which has two public member variables $name and $age . Then we instantiate an object $person and assign it to the variable $person.

Next, we use the (array) operator to convert the object $person into an array, and assign the variable $arr to this array . Finally, we use the print_r() function to print the elements in the array $arr. The results are as follows:

Array
(
    [name] => Tom
    [age] => 25
)

As you can see, the key names in the array $arr are name and age, corresponding to the object ## Public member variables in #$person.

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:
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