array_values
array_values(array $array): array
Returns all values of an array (rebuilding numeric indexes).
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} };
size_t n = sizeof scores / sizeof scores[0];
printf("[");
for (size_t i = 0; i < n; i++) {
printf(i ? ", %d" : "%d", scores[i].val);
}
printf("]\n");
return 0;
}
Output
[1, 2, 3]
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<int> values;
for (const auto& [k, v] : scores) values.push_back(v); // structured bindings to fetch values
std::cout << "[";
for (size_t i = 0; i < values.size(); ++i) {
if (i) std::cout << ", ";
std::cout << values[i];
}
std::cout << "]\n";
return 0;
}
Output
[1, 2, 3]
C# v11.0.100
v11.0.100
var scores = new Dictionary<string, int> { ["a"] = 1, ["b"] = 2, ["c"] = 3 };
Console.WriteLine($"[{string.Join(", ", scores.Values)}]");
Output
[1, 2, 3]
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)
vals := make([]int, 0, len(m))
for _, k := range keys {
vals = append(vals, m[k])
}
fmt.Println(vals)
}
Output
[1 2 3]
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.values());
}
}
Output
[1, 2, 3]
JavaScript v26.13.1
v26.13.1
console.log(Object.values({ a: 1, b: 2, c: 3 }))
Output
[ 1, 2, 3 ]
PHP v8.6.0
v8.6.0
echo json_encode(array_values(["a" => 1, "b" => 2, "c" => 3]));
Output
[1,2,3]
Python v3.15.2
v3.15.2
print(list({"a": 1, "b": 2, "c": 3}.values()))
Output
[1, 2, 3]
Rust v1.99.1
v1.99.1
fn main() {
let m = std::collections::BTreeMap::from([("a", 1), ("b", 2), ("c", 3)]);
let vals: Vec<_> = m.values().collect();
println!("{:?}", vals);
}
Output
[1, 2, 3]
TypeScript v26.13.1
v26.13.1
const scores: Record<string, number> = { a: 1, b: 2, c: 3 };
console.log(Object.values(scores));
Output
[ 1, 2, 3 ]
Language differences
C: has no built-in containers or higher-order functions; the example uses fixed arrays/buffers and hand-written loops.
C++: collects values by iterating the second component of std::map.
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 before taking values for stable output.
Java: LinkedHashMap preserves insertion order.
JavaScript: uses Object.values.
PHP: array_values rebuilds numeric indexes starting from 0.
Rust: BTreeMap iterates in key order.