array_column
array_column(array $array, int|string|null $column_key, int|string|null $index_key = null): array
Extracts the values of a given column from a two-dimensional array.
C C23
C23
#include <stdio.h>
typedef struct { int id; const char *name; } Row;
int main(void) {
Row rows[] = { {1, "a"}, {2, "b"} };
size_t n = sizeof rows / sizeof rows[0];
printf("["); // extract the name column: C has no higher-order functions, hand-written loop
for (size_t i = 0; i < n; i++) {
printf(i ? ", \"%s\"" : "\"%s\"", rows[i].name);
}
printf("]\n");
return 0;
}
Output
["a", "b"]
C++ C++26
C++26
#include <iostream>
#include <string>
#include <vector>
struct Row { int id; std::string name; };
int main() {
std::vector<Row> rows = { {1, "a"}, {2, "b"} };
std::vector<std::string> names;
for (const auto& r : rows) names.push_back(r.name); // extract the name column
std::cout << "[";
for (size_t i = 0; i < names.size(); ++i) {
if (i) std::cout << ", ";
std::cout << '"' << names[i] << '"';
}
std::cout << "]\n";
return 0;
}
Output
["a", "b"]
C# v11.0.100
v11.0.100
var rows = new List<(int Id, string Name)> { (1, "a"), (2, "b") };
// LINQ Select extracts one column, equivalent to PHP array_column
var names = rows.Select(r => r.Name).ToList();
Console.WriteLine($"[{string.Join(", ", names.Select(n => $"\"{n}\""))}]");
Output
["a", "b"]
Go v1.27.4
v1.27.4
package main
import "fmt"
type Row struct {
ID int
Name string
}
func main() {
rows := []Row{{ID: 1, Name: "a"}, {ID: 2, Name: "b"}}
names := make([]string, 0, len(rows))
for _, r := range rows {
names = append(names, r.Name)
}
fmt.Println(names)
}
Output
[a b]
Java v27.0.1
v27.0.1
import java.util.List;
public class Main {
static class Row {
int id;
String name;
Row(int i, String n) { id = i; name = n; }
}
public static void main(String[] args) {
List<Row> rows = List.of(new Row(1, "a"), new Row(2, "b"));
System.out.println(rows.stream().map(r -> r.name).toList());
}
}
Output
[a, b]
JavaScript v26.13.1
v26.13.1
console.log([{ id: 1, name: "a" }, { id: 2, name: "b" }].map(o => o.name))
Output
[ 'a', 'b' ]
PHP v8.6.0
v8.6.0
echo json_encode(array_column([["id" => 1, "name" => "a"], ["id" => 2, "name" => "b"]], "name"));
Output
["a","b"]
Python v3.15.2
v3.15.2
rows = [{"id": 1, "name": "a"}, {"id": 2, "name": "b"}]
print([d["name"] for d in rows])
Output
['a', 'b']
Rust v1.99.1
v1.99.1
fn main() {
let rows = vec![(1, "a"), (2, "b")];
let names: Vec<_> = rows.iter().map(|&(_, name)| name).collect();
println!("{:?}", names);
}
Output
["a", "b"]
TypeScript v26.13.1
v26.13.1
const rows: { id: number; name: string }[] = [
{ id: 1, name: "a" },
{ id: 2, name: "b" },
];
console.log(rows.map((o) => o.name));
Output
[ 'a', 'b' ]
Language differences
C: has no built-in containers or higher-order functions; the example uses fixed arrays/buffers and hand-written loops.
C++: uses an array/vector of structs as rows and extracts the column in a loop.
Go: requires defining a struct (or tuple) first and then mapping over it to pick a field.
Java: requires defining a struct (or tuple) first and then mapping over it to pick a field.
JavaScript: uses the general-purpose alternative map.
PHP: provides it specifically for column extraction.
Python: uses the general-purpose alternative of a list comprehension.
Rust: requires defining a struct (or tuple) first and then mapping over it to pick a field.