PHP array_combine: How to Merge Arrays as Key and Value

php array combine

The array_combine function in PHP creates a new array with one array for keys and another for values.

It needs both arrays to have the same size and order, or the function will not work.

You can use it when you want to connect two arrays in a single structure.
The first array sets the keys, and the second array sets the values.

Understand the array_combine in PHP

The function creates an array from two arrays, where the first sets the keys and the second sets the values.

Here is the syntax:

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

The function takes two arrays and returns one array.

Here are the reasons to use it:

  • You may want to build a new array from two lists.
  • You can use names as keys and numbers as values.
  • You can also map data from one source to another.

For a quick example:

$names = ["Tom", "Sara", "Mike"];
$ages = [25, 30, 22];
$result = array_combine($names, $ages);
print_r($result);

This example shows that the keys are names and the values are ages.
Hence, the new array looks like:

Array
(
[Tom] => 25
[Sara] => 30
[Mike] => 22
)

Examples of array_combine with Different Arrays in PHP

Create Product and Price Array:

$products = ["Book", "Pen", "Bag"];
$prices = [10, 2, 25];
$result = array_combine($products, $prices);
print_r($result);

This builds an array where each product has a price value. Here is the output:

Array
(
[Book] => 10
[Pen] => 2
[Bag] => 25
)

Map Country and Code:

$countries = ["USA", "UK", "India"];
$codes = ["US", "GB", "IN"];
$result = array_combine($countries, $codes);
print_r($result);

This connects each country with its code in one array. The output:

Array
(
[USA] => US
[UK] => GB
[India] => IN
)

Match Subject and Marks:

$subjects = ["Math", "English", "Science"];
$marks = [85, 90, 78];
$result = array_combine($subjects, $marks);
print_r($result);

This shows each subject with its mark in the array. Here is the output:

Array
(
[Math] => 85
[English] => 90
[Science] => 78
)

Combine Days and Numbers:

$days = ["Mon", "Tue", "Wed"];
$numbers = [1, 2, 3];
$result = array_combine($days, $numbers);
print_r($result);

This creates an array where days of the week link with numbers. The output:

Array
(
[Mon] => 1
[Tue] => 2
[Wed] => 3
)

Wrapping Up

You learned what array_combine does and how it works with two arrays.

Here is a quick recap:

  • The function takes one array for keys and one for values.
    Both arrays must have the same length. The function will fail if they don’t have the same length.
  • You can use it for names and ages or products and prices. Also, use it with any two lists.

FAQs

What does php array_combine do with arrays?

php array_combine creates a new array using one array for keys and another array for values. Example:
$keys = array("a", "b", "c");
$values = array(1, 2, 3);
$result = array_combine($keys, $values);
print_r($result);
Output: Array ( [a] => 1 [b] => 2 [c] => 3 )

What happens if arrays have different lengths in php array_combine?

array_combine requires both arrays to have the same number of elements. If not, it will throw an Fatal error. Example:
$keys = array("x", "y");
$values = array(10);
$result = array_combine($keys, $values);
Output: Fatal error: array_combine(): Both parameters should have an equal number of elements

How can you use php array_combine in real projects?

You can use php array_combine to map IDs to names, settings, or structured data. Example:
$ids = array(101, 102, 103);
$names = array("Ali", "Sara", "Omar");
$result = array_combine($ids, $names);
print_r($result);
Output: Array ( [101] => Ali [102] => Sara [103] => Omar )
  • Map user IDs with usernames
  • Create lookup tables
  • Build config arrays

Similar Reads

PHP strpos Function: How it Works with Examples

Sometimes the string you expect is there, but strpos function gives a surprise in PHP. It may show the result…

PHP MySQL WHERE: How to Filter Data in MySQL

Filtering data is a big part of getting the right information to show up. That is where the WHERE clause…

PHP str_repeat: How it Works with Examples

PHP gives you many string tools. One of them is php str_repeat. This function repeats a texts as many times…

PHP filter_var_array: How to Validate Multiple Inputs

The filter_var_array() appeared to make input validation simple and sanitization in PHP. It handles multiple inputs with different filters was…

PHP Arrow Functions: Understanding “fn” Syntax

Arrow functions were introduced in PHP 7.4. They offer you a way to write simple operations, such as calculations, filters,…

PHP array_diff_ukey Function: How it Works with Examples

The PHP array_diff_ukey function compares keys in two or more arrays. It returns the keys from the first array that…

Traits in PHP: What They Are & Examples

PHP introduced traits to solve a problem with code reuse. Before traits, developers used inheritance and interfaces to share functionality…

PHP Type Hints: Ensuring Data Integrity

PHP-type hinting is something that keeps your code in check and helps your functions receive just the right types of…

PHP Bitwise Operators: AND, OR, XOR, NOT with Examples

Bitwise operators use symbols like &, |, and ^, and they work at the binary level. But once you understand…

PHP Filters: How to Validate and Sanitize User Inputs

User input can be unpredictable and dangerous. A simple form submission might carry malicious code or invalid data and lead…

Previous Article

HTML kbd Tag: How to Show Keyboard Input with examples

Next Article

JavaScript Object Methods with Examples

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *


Subscribe to Get Updates

Get the latest updates on Coding, Database, and Algorithms straight to your inbox.
No spam. Unsubscribe anytime.