Home > Backend Development > PHP Problem > How to convert variable to array in php

How to convert variable to array in php

青灯夜游
Release: 2023-03-10 22:12:01
Original
3658 people have browsed it

In PHP, you can use the compact() function to convert one or more variables into an array. This function can create an array containing variable names and their values. The syntax format is "compact(var1,var2.. .)".

How to convert variable to array in php

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

php converts variables into arrays

<?php
header("Content-type:text/html;charset=utf-8");
//多个变量转数组
$name = &#39;php中文网&#39;;
$url = &#39;www.php.cn&#39;;
$info = compact(&#39;name&#39;, &#39;url&#39;);
//传递变量名
print_r($info);
?>
Copy after login

Output:

rray ( [name] => php中文网 [url] => www.php.cn )
Copy after login

Description:

compact() function creates an array containing variable names and their values.

Any string that does not have a corresponding variable name is ignored.

Syntax

compact(var1,var2...)
Copy after login
Parameters Description
var1 Required. Can be a string with a variable name, or an array of variables.
var2,... Optional. Can be a string with a variable name, or an array of variables. Multiple parameters are allowed.

Return value: Returns an array with all variable names and their values.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to convert variable to array in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template