array_keys
array_keys(array $array): array
Returns all keys of an array.
C C23
C23
#include <stdio.h>
typedef struct { const char *key; int val; } Pair;
int main(void) {
Pair scores[] = { {"a", 1}, {"b", 2}, {"c", 3} }; // assoc array simulated with a struct
size_t n = sizeof scores / sizeof scores[0];
printf("[");
for (size_t i = 0; i < n; i++) {
printf(i ? ", \"%s\"" : "\"%s\"", scores[i].key);
}
printf("]\n");
return 0;
}
Output
["a", "b", "c"]
C++ C++26
C++26
#include <iostream>
#include <map>
#include <string>
#include <vector>
int main() {
std::map<std::string, int> scores = { {"a", 1}, {"b", 2}, {"c", 3} };
std::vector<std::string> keys;
for (const auto& [k, v] : scores) keys.push_back(k); // structured bindings to fetch keys
std::cout << "[";
for (size_t i = 0; i < keys.size(); ++i) {
if (i) std::cout << ", ";
std::cout << '"' << keys[i] << '"';
}
std::cout << "]\n";
return 0;
}
Output
["a", "b", "c"]
C# v11.0.100
v11.0.100
var scores = new Dictionary<string, int> { ["a"] = 1, ["b"] = 2, ["c"] = 3 };
Console.WriteLine($"[{string.Join(", ", scores.Keys.Select(k => $"\"{k}\""))}]");
Output
["a", "b", "c"]
Go v1.27.4
v1.27.4
package main
import (
"fmt"
"sort"
)
func main() {
m := map[string]int{"a": 1, "b": 2, "c": 3}
keys := make([]string, 0, len(m))
for k := range m {
keys = append(keys, k)
}
sort.Strings(keys)
fmt.Println(keys)
}
Output
[a b c]
Java v27.0.1
v27.0.1
import java.util.LinkedHashMap;
import java.util.Map;
public class Main {
public static void main(String[] args) {
Map<String, Integer> m = new LinkedHashMap<>();
m.put("a", 1);
m.put("b", 2);
m.put("c", 3);
System.out.println(m.keySet());
}
}
Output
[a, b, c]
JavaScript v26.13.1
v26.13.1
console.log(Object.keys({ a: 1, b: 2, c: 3 }))
Output
[ 'a', 'b', 'c' ]
PHP v8.6.0
v8.6.0
echo json_encode(array_keys(["a" => 1, "b" => 2, "c" => 3]));
Output
["a","b","c"]
Python v3.15.2
v3.15.2
print(list({"a": 1, "b": 2, "c": 3}.keys()))
Output
['a', 'b', 'c']
Rust v1.99.1
v1.99.1
fn main() {
let m = std::collections::BTreeMap::from([("a", 1), ("b", 2), ("c", 3)]);
let keys: Vec<_> = m.keys().collect();
println!("{:?}", keys);
}
Output
["a", "b", "c"]
TypeScript v26.13.1
v26.13.1
const scores: Record<string, number> = { a: 1, b: 2, c: 3 };
console.log(Object.keys(scores));
Output
[ 'a', 'b', 'c' ]
Language differences
C: has no built-in containers or higher-order functions; the example uses fixed arrays/buffers and hand-written loops.
C++: std::map corresponds to an associative array; the structured binding [k, v] iterates key/value pairs.
C#: Dictionary enumerates in insertion order in current .NET, but the API contract does not guarantee that order.
Go: maps are unordered, so the example sorts the keys for deterministic output.
Java: HashMap is unordered while LinkedHashMap preserves insertion order (the example uses the latter).
PHP: associative arrays preserve insertion order.
Rust: BTreeMap iterates in key order.