Current Location: Home> Latest Articles> The difference between using compact() and application scenario analysis

The difference between using compact() and application scenario analysis

M66 2025-06-07

In PHP, array_combine() and compact() are commonly used array operation functions, and they each have different functions and application scenarios. This article will analyze in detail the differences between these two functions and the scenarios they are suitable for use.

1. array_combine() function

The array_combine() function is used to combine two arrays into an associative array. Specifically, the first array provides the keys of the associative array, and the second array provides the values ​​corresponding to these keys. If the number of elements of the two arrays is different, array_combine() will return false .

grammar:
 array_combine(array $keys, array $values): array|false
Example:
 $keys = ['a', 'b', 'c'];
$values = [1, 2, 3];

$result = array_combine($keys, $values);
print_r($result);

Output result:

 Array
(
    [a] => 1
    [b] => 2
    [c] => 3
)

In this example, each element of the $keys array becomes the key of the associative array, and the element of the $values ​​array becomes the value of the corresponding key.

Applicable scenarios:
  • array_combine() is a very convenient option when you need to create an associative array through two independent arrays (one is a key and the other is a value).

2. compact() function

The compact() function is used to create an associative array containing variable names and values. It accepts one or more variable names and returns an array where the key is the variable name and the value is the value of those variables. compact() is often used to generate an array of multiple variables in the current scope.

grammar:
 compact(string|array $var_name, string ...$var_names): array
Example:
 $name = "John";
$age = 30;
$city = "New York";

$result = compact('name', 'age', 'city');
print_r($result);

Output result:

 Array
(
    [name] => John
    [age] => 30
    [city] => New York
)

In this example, the compact() function generates an associative array containing name , age , and city variables.

Applicable scenarios:
  • The compact() function is very useful when you have multiple variables and want to organize them in an array. For example, when generating data for view templates, or when processing requested data, it can conveniently package multiple variables into an array.

The main differences between array_combine() and compact()

  1. Parameter type :

    • array_combine() requires two arrays as parameters, one for generating the keys of the array and the other for generating the value of the array.

    • compact() requires passing one or more variable names (strings) as parameters, and then returning the associative array corresponding to these variable names.

  2. use :

    • array_combine() is suitable for scenarios where two arrays are combined into one associative array.

    • compact() is suitable for scenarios where variables and their values ​​in the current scope are packaged into an associative array.

  3. Return result :

    • array_combine() returns an associative array that corresponds to keys and values.

    • compact() returns an associative array containing the variable name and the variable value.

When to use array_combine() and compact() ?

  • Scenarios using array_combine() :

    • When you have two independent arrays and want to merge them into one associative array, using array_combine() is very suitable. For example, pair the field name with its corresponding value, create a data model, etc.

  • Scenarios using compact() :

    • When you need to convert multiple variables into an array, it is more convenient to use compact() . For example, when you package multiple variables into an array in a controller and pass them to a view, or when you record multiple variable values ​​in a log, you can use compact() .

Summarize

  • array_combine() is an associative array that combines two arrays together to form a key-value pair.

  • compact() is an associative array that converts multiple variables into key-value pairs. The key is the variable name and the value is the value of the variable.

  • Depending on the needs, choosing the right function can improve the readability and efficiency of the code.