Home  >  Article  >  Backend Development  >  Introduction to the usage of PHP array_combine() function

Introduction to the usage of PHP array_combine() function

王林
王林Original
2023-06-27 16:35:141745browse

PHP is a very popular programming language that provides programmers with many practical functions and methods. One of the very practical functions is the array_combine() function. The array_combine() function combines two arrays into an associative array and returns it. It has many practical application scenarios.

1. The syntax of array_combine() function

The syntax of array_combine() function is as follows:

array_combine(array $keys, array $values) : array

Among them, $keys is an array containing key names, and $values ​​is an array containing key values. The array_combine() function will return a new array. The keys of this new array will be the values ​​in the $keys array, and the values ​​will be the values ​​at the corresponding positions in the $values ​​array.

If the number of elements in the two arrays is different, the array_combine() function will return false and log an error in the error log.

2. Application scenarios of array_combine() function

The application scenarios of array_combine() function are very wide. Below we will introduce several common usage methods:

  1. Create a new associative array

Use the array_combine() function to create a new associative array. The key name of this array comes from one array and the key value comes from another array. For example:

$keys = array('name', 'age', 'sex');
$values = array('Tom', '25', 'male');
$arr = array_combine($keys, $values);
print_r($arr);

The output result is:

Array
(
    [name] => Tom
    [age] => 25
    [sex] => male
)
  1. Update associative array

Use the array_combine() function to replace the value of one array with another An array of values, thereby updating an associative array. For example:

$arr1 = array('name'=>'Tom', 'age'=>25, 'sex'=>'male');
$arr2 = array('age'=>26);
$new_arr = array_combine(array_keys($arr1), array_replace($arr1, $arr2));
print_r($new_arr);

The output result is:

Array
(
    [name] => Tom
    [age] => 26
    [sex] => male
)
  1. Processing form submission data

In front-end development, we usually use forms to collect user input The data. Using the array_combine() function, we can process the data submitted by the form, use the name attribute of the form control as the key name, and the value entered by the user as the key value, thereby generating an associative array. For example:

if($_SERVER['REQUEST_METHOD'] == 'POST') {
    $keys = array_keys($_POST);
    $values = array_values($_POST);
    $arr = array_combine($keys, $values);
    // 对表单数据进行处理
}
  1. Convert a two-dimensional array to an associative array

When you need to convert a two-dimensional array into an associative array, you can use the array_combine() function. For example:

$data = array(
    array('name'=>'Tom', 'age'=>25),
    array('name'=>'Jerry', 'age'=>30),
    array('name'=>'Kate', 'age'=>28)
);
$new_data = array();
foreach($data as $item) {
    $new_data[] = array_combine(array_keys($item), array_values($item));
}
print_r($new_data);

The output result is:

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

    [1] => Array
        (
            [name] => Jerry
            [age] => 30
        )

    [2] => Array
        (
            [name] => Kate
            [age] => 28
        )

)

3. Summary

PHP’s array_combine() function is a very practical function that can combine two arrays into An associative array, and is used in a variety of scenarios, such as creating new associative arrays, updating associative arrays, processing form submission data, converting two-dimensional arrays to associative arrays, etc. Mastering the usage of the array_combine() function can improve programmers' coding efficiency. If you want to learn PHP programming well, mastering this function is essential.

The above is the detailed content of Introduction to the usage of PHP array_combine() function. 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