Home  >  Article  >  Backend Development  >  How to Combine Two Arrays into a New Array with Specific Key-Value Pairs in PHP?

How to Combine Two Arrays into a New Array with Specific Key-Value Pairs in PHP?

Susan Sarandon
Susan SarandonOriginal
2024-11-05 14:48:02603browse

How to Combine Two Arrays into a New Array with Specific Key-Value Pairs in PHP?

How to Pivot Arrays Using array_combine()

Often when working with data, it becomes necessary to combine two arrays in a specific way. For instance, if you have two arrays, one containing keys and the other containing values, you may want to create a new array using the keys from the first array as the keys for the new array and the values from the second array as the corresponding values. This operation is known as array pivoting.

array_combine() to the Rescue

The PHP function array_combine() is designed specifically for this purpose. It takes two arrays as parameters: one containing the keys and the other containing the values, and returns a new array with the specified key-value pairs.

Example Usage

Let's illustrate the usage of array_combine() with the following example:

<code class="php">$array['A'] = ['cat', 'bat', 'hat', 'mat'];
$array['B'] = ['fur', 'ball', 'clothes', 'home'];
$array['C'] = array_combine($array['A'], $array['B']);</code>

Output:

Array (
    [cat] => "fur"
    [bat] => "ball"
    [hat] => "clothes"
    [mat] => "home"
)

Explanation

In this example, the array_combine() function is used to create a new array $array['C'] by combining the values from $array['A'] as keys and the values from $array['B'] as the corresponding values. The resulting array has the keys from $array['A'] as the keys for each element and the corresponding values from $array['B'] as the values for each element.

Advantages of array_combine()

  • Simplicity: array_combine() provides a simple and straightforward way to pivot arrays and create a new array with the desired key-value pairs.
  • Performance: array_combine() is generally faster than using loops or other more complex methods to manually combine arrays.
  • Versatility: array_combine() can be used to combine any two arrays, regardless of the data types they contain.

While using array_combine() is generally the quickest and easiest way to pivot arrays, it's worth noting that other methods, such as loops or custom functions, can also be used if the situation requires more flexibility or customization. However, for most common use cases, array_combine() is the preferred approach for its simplicity and performance benefits.

The above is the detailed content of How to Combine Two Arrays into a New Array with Specific Key-Value Pairs 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