Home > Backend Development > PHP Problem > How to use compact to create an array in PHP

How to use compact to create an array in PHP

autoload
Release: 2023-03-09 12:08:02
Original
1555 people have browsed it

How to use compact to create an array in PHP

PHPTo create an array, array() is usually used, but in fact PHP also provides other The function creates an array. This article will show you how to use the compact() function to create an array.

First, let’s take a look at the syntax of the compact() function:

compact    ( mixed $var_name   , mixed ...$var_names   )
Copy after login
  • $var_name: compact() Accepts variable parameters number. Each parameter can be a string containing a variable name or an array containing a variable name. The array can also contain other arrays whose unit contents are variable names. compact() can be processed recursively.

  • Return value: Returns the output array, including all added variables.

Code example:

<?php
$city  = "HeFei";
$province = "AnHui";
$conty = "What";

$location = array("city", "province");
print_r($location);
echo "<br>";
$result = compact("conty", $location);
print_r($result);
?>
Copy after login
输出:
Array ( [0] => city [1] => province )
Array ( [conty] => What [city] => HeFei [province] => AnHui )
Copy after login

Recommendation: 2021 PHP Interview Questions Summary (Collection) 》《php video tutorial

The above is the detailed content of How to use compact to create an array in PHP. For more information, please follow other related articles on the PHP Chinese website!

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