Home  >  Article  >  Backend Development  >  How to combine multiple arrays into one associative array in PHP

How to combine multiple arrays into one associative array in PHP

WBOY
WBOYOriginal
2023-07-07 21:46:54711browse

How to merge multiple arrays into an associative array in PHP

In PHP, merging multiple arrays is a very common operation. Merging multiple arrays into an associative array is a more special and flexible requirement. This article will introduce how to use PHP to combine multiple arrays into an associative array.

There are two functions in PHP that can be used to merge arrays, namely the array_merge() and array_merge_recursive() functions. But they do not fully meet the requirements of merging multiple arrays into an associative array. Therefore, we need a custom function to implement this function.

The following is a way to combine multiple arrays into one associative array:

function mergeArrays($keys, $values) {
    $result = array();

    foreach ($keys as $index => $key) {
        if (isset($values[$index])) {
            $result[$key] = $values[$index];
        }
    }

    return $result;
}

The above function accepts two parameters: $keys and $values. $keys is an array containing associative arrays, each associative array represents an array of key names. And $values ​​is an array containing associative arrays, each associative array represents an array of key values. The function is to combine the arrays at corresponding positions in $keys and $values ​​into an associative array.

The following is a sample code using the above function:

$keys1 = ['name', 'age', 'gender'];
$values1 = ['John', 25, 'male'];

$keys2 = ['name', 'email', 'phone'];
$values2 = ['Jane', 'jane@example.com', '123456789'];

$result = mergeArrays([$keys1, $keys2], [$values1, $values2]);

var_dump($result);

Run the above code, you will get the following output:

array(6) {
  ["name"]=>
  string(4) "Jane"
  ["age"]=>
  int(25)
  ["gender"]=>
  string(4) "male"
  ["email"]=>
  string(16) "jane@example.com"
  ["phone"]=>
  string(9) "123456789"
}

The above output result is the association between $keys1 and $values1 The arrays are merged into the $result array, and then the associative arrays of $keys2 and $values2 are merged into the $result array. You end up with an associative array that contains all associative arrays.

Summary:

This article introduces how to merge multiple arrays into an associative array in PHP. Through custom functions, we can implement flexible array merging operations. I hope that readers can better master the array operation skills in PHP and improve programming efficiency through the introduction of this article.

The above is the detailed content of How to combine multiple arrays into one associative 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