array_combine
array_combine(array $keys, array $values): array
Creates a map array using one array as keys and the other as values; both arrays must have equal length.
C C23
#include <stdio.h>
int main(void) {
const char *keys[] = {"a", "b", "c"};
int values[] = {1, 2, 3};
size_t n = sizeof keys / sizeof keys[0];
for (size_t i = 0; i < n; i++) { // no dict type: parallel arrays are the C idiom
printf("%s=%d\n", keys[i], values[i]);
}
return 0;
}
a=1 b=2 c=3
C++ C++26
#include <iostream>
#include <string>
#include <vector>
int main() {
std::vector<std::string> keys = {"a", "b", "c"};
std::vector<int> values = {1, 2, 3};
for (size_t i = 0; i < keys.size(); ++i) { // parallel vectors; C++23 ranges::zip pairs them lazily
std::cout << keys[i] << "=" << values[i] << "\n";
}
return 0;
}
a=1 b=2 c=3
C# v11.0.100
var keys = new[] { "a", "b", "c" };
var values = new[] { 1, 2, 3 };
var map = keys.Zip(values).ToDictionary(p => p.First, p => p.Second); // Zip pairs by index
Console.WriteLine("{" + string.Join(", ", map.Select(kv => $"{kv.Key}: {kv.Value}")) + "}");
{a: 1, b: 2, c: 3}
Go v1.27.4
package main
import "fmt"
func main() {
keys := []string{"a", "b", "c"}
values := []int{1, 2, 3}
m := make(map[string]int)
for i, k := range keys {
m[k] = values[i]
}
fmt.Println(m) // fmt prints maps sorted by key (deterministic)
}
map[a:1 b:2 c:3]
Java v27.0.1
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
public class Main {
public static void main(String[] args) {
List<String> keys = List.of("a", "b", "c");
List<Integer> values = List.of(1, 2, 3);
Map<String, Integer> map = new LinkedHashMap<>(); // keeps insertion order
for (int i = 0; i < keys.size(); i++) {
map.put(keys.get(i), values.get(i));
}
System.out.println(map);
}
}
{a=1, b=2, c=3}
JavaScript v26.13.1
const keys = ["a", "b", "c"];
const values = [1, 2, 3];
console.log(Object.fromEntries(keys.map((k, i) => [k, values[i]])));
{ a: 1, b: 2, c: 3 }
PHP v8.6.0
echo json_encode(array_combine(['a', 'b', 'c'], [1, 2, 3])); // PHP arrays are ordered maps already
{"a":1,"b":2,"c":3}
Python v3.15.2
print(dict(zip(['a', 'b', 'c'], [1, 2, 3]))) # zip pairs by position; dict keeps insertion order
{'a': 1, 'b': 2, 'c': 3}
Rust v1.99.1
fn main() {
let keys = ["a", "b", "c"];
let values = [1, 2, 3];
let pairs: Vec<_> = keys.iter().zip(values.iter()).collect(); // zip + collect
println!("{:?}", pairs);
}
[("a", 1), ("b", 2), ("c", 3)]
TypeScript v26.13.1
const keys: string[] = ["a", "b", "c"];
const values: number[] = [1, 2, 3];
const map: Record<string, number> = Object.fromEntries(
keys.map((k, i) => [k, values[i]]),
);
console.log(map);
{ a: 1, b: 2, c: 3 }
Language differences
C: no dict literal — parallel arrays indexed side by side are the essence of "combine".
C++: no dict literal — parallel arrays indexed side by side are the essence of "combine".
C#: uses keys.Zip(values).ToDictionary(...) (Zip truncates; duplicate keys throw).
Go: no zip, pair by index in a loop; maps are unordered but fmt prints them sorted by key (accidentally deterministic), and if your logic depends on order, keep a separate key slice when iterating.
Java: no zip, pair by index in a loop, with a LinkedHashMap to keep insertion order (HashMap is unordered).
JavaScript: Object.fromEntries(keys.map((k, i) => [k, values[i]])) (you must guarantee equal lengths yourself); objects keep insertion order.
PHP: array_combine($keys, $values) builds a key-value map; mismatched lengths throw ValueError as of PHP 8 (false + warning in PHP 7 and earlier), and keys must be valid array keys (int/string, with bool/float truncated, null becoming the empty string); the array itself is an ordered map and naturally keeps insertion order; JSON serialization conventions differ (PHP without spaces, JS console with spaces, Go printing without commas) while the values agree.
Python: dict(zip(...)) is a one-liner (zip truncates to the shorter input — more forgiving than PHP); dict keeps insertion order (naturally ordered).
Rust: collects iter().zip() into a Vec/HashMap/BTreeMap — the example collects a Vec so printing is deterministic (HashMap print order is random, and teaching examples need determinism).