array_key_first
array_key_first(array $array): int|string|null
Gets the first key of an array without moving the internal pointer.
C C23
#include <stdio.h>
// C has no built-in associative array; a struct array + index 0 gives the first key
struct Entry {
const char *name;
int age;
};
int main(void) {
struct Entry ages[] = {{"Peter", 35}, {"Ben", 37}, {"Joe", 43}};
printf("%s\n", ages[0].name);
return 0;
}
Peter
C++ C++26
#include <iostream>
#include <map>
#include <string>
int main() {
// unordered_map is unordered; std::map sorts by key ascending, so begin() is the smallest key
std::map<std::string, int> ages{
{"Peter", 35}, {"Ben", 37}, {"Joe", 43}};
std::cout << ages.begin()->first << "\n";
return 0;
}
Ben
C# v11.0.100
using System.Collections.Generic;
// Dictionary is unordered; an ordered mapping is idiomatically a list of key-value pairs
var ages = new List<KeyValuePair<string, int>>
{
new("Peter", 35), new("Ben", 37), new("Joe", 43),
};
Console.WriteLine(ages[0].Key);
Peter
Go v1.27.4
package main
import "fmt"
func main() {
// map iteration order is randomized, so "the first key" is meaningless;
// for ordered scenarios, a slice of key-value pairs is idiomatic
ages := []struct {
name string
age int
}{{"Peter", 35}, {"Ben", 37}, {"Joe", 43}}
fmt.Println(ages[0].name)
}
Peter
Java v27.0.1
import java.util.LinkedHashMap;
import java.util.Map;
public class Main {
public static void main(String[] args) {
// HashMap is unordered; LinkedHashMap keeps insertion order, so the iterator yields the first key
Map<String, Integer> ages = new LinkedHashMap<>();
ages.put("Peter", 35);
ages.put("Ben", 37);
ages.put("Joe", 43);
System.out.println(ages.keySet().iterator().next());
}
}
Peter
JavaScript v26.13.1
// Object.keys returns the key array; take the first one
const ages = { Peter: 35, Ben: 37, Joe: 43 };
console.log(Object.keys(ages)[0]);
// An empty object yields undefined
console.log(Object.keys({})[0]);
Peter undefined
PHP v8.6.0
// Available since PHP 7.3: returns the first key without moving the array's internal pointer
$ages = ["Peter" => 35, "Ben" => 37, "Joe" => 43];
echo array_key_first($ages), "\n";
// An empty array returns null
var_dump(array_key_first([]));
Peter NULL
Python v3.15.2
# dicts preserve order (a language guarantee since 3.7): next(iter(d)) gets the first key
ages = {"Peter": 35, "Ben": 37, "Joe": 43}
print(next(iter(ages)))
# An empty dict falls back to the default value
print(next(iter({}), None))
Peter None
Rust v1.99.1
use std::collections::BTreeMap;
fn main() {
// HashMap iterates unordered; BTreeMap iterates by key order, so the first key is available (1.66+)
let mut ages = BTreeMap::new();
ages.insert("Peter", 35);
ages.insert("Ben", 37);
ages.insert("Joe", 43);
println!("{}", ages.first_key_value().unwrap().0);
}
Ben
TypeScript v26.13.1
// Object.keys returns the key array; take the first one
const ages: Record<string, number> = { Peter: 35, Ben: 37, Joe: 43 };
console.log(Object.keys(ages)[0]);
// An empty object yields undefined
console.log(Object.keys({})[0]);
Peter undefined
Language differences
C: no built-in associative array — use a struct array and take index 0.
C++: std::map orders by key ascending, so begin()->first is the smallest key; unordered_map iterates unordered, and keeping the insertion order needs extra bookkeeping.
C#: Dictionary<TKey,TValue> iteration order is unspecified; ordered mappings conventionally use a key-value list or a third-party ordered dictionary.
Go: map iteration order is deliberately randomized, so "first key" is meaningless; the convention is a slice of key-value pairs.
Java: LinkedHashMap preserves insertion order and keySet().iterator().next() yields the first key; HashMap is unordered.
JavaScript: object keys keep insertion order (except integer-like keys) — Object.keys($array)[0].
PHP: PHP arrays themselves are ordered hash tables — the biggest semantic difference from the languages above; array_key_first (PHP 7.3) replaces the old reset($array) + key($array) dance (moves the internal pointer and warns on empty arrays).
Python: dict preserves insertion order by language guarantee since 3.7; next(iter(d)) gets the first key.
Rust: HashMap is unordered; BTreeMap::first_key_value (1.66+) returns the smallest key in key order — semantics are "minimum key", not "first inserted".
TypeScript: same as JavaScript's Object.keys.