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
autoloadOriginal
2021-04-20 12:50:571554browse

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   )
  • $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);
?>
输出:
Array ( [0] => city [1] => province )
Array ( [conty] => What [city] => HeFei [province] => AnHui )

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!

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