array_all / array_any / array_find …
array_all(array $array, callable $callback): bool — array_any(array $array, callable $callback): bool — array_find(array $array, callable $callback): mixed — array_find_key(array $array, callable $callback): mixed — array_first(array $array): mixed — array_last(array $array): mixed
PHP 8.4's modern callback quartet (array_all/array_any/array_find/array_find_key - callback takes value and key, short-circuits) plus 8.5's array_first/array_last, each with its native counterpart in other languages.
C C23
#include <stdio.h>
/* hand-written counterparts: the predicate takes (value, index) */
static int all(const int *xs, int n, int (*pred)(int, int)) {
for (int i = 0; i < n; i++) {
if (!pred(xs[i], i)) return 0;
}
return 1;
}
static int any(const int *xs, int n, int (*pred)(int, int)) {
for (int i = 0; i < n; i++) {
if (pred(xs[i], i)) return 1;
}
return 0;
}
/* returns the index of the first hit, or -1 (stands in for array_find_key) */
static int find(const int *xs, int n, int (*pred)(int, int)) {
for (int i = 0; i < n; i++) {
if (pred(xs[i], i)) return i;
}
return -1;
}
static int gt40(int v, int i) { (void)i; return v > 40; }
static int lt50(int v, int i) { (void)i; return v < 50; }
static int gt80(int v, int i) { (void)i; return v > 80; }
int main(void) {
int xs[] = {92, 47, 88};
int n = (int)(sizeof xs / sizeof xs[0]);
int idx = find(xs, n, gt80);
printf("%d\n", all(xs, n, gt40));
printf("%d\n", any(xs, n, lt50));
printf("%d\n", idx >= 0 ? xs[idx] : -1);
printf("%d\n", idx);
printf("%d\n", xs[0]);
printf("%d\n", xs[n - 1]);
return 0;
}
1 1 92 0 92 88
C++ C++26
#include <iostream>
#include <vector>
/* hand-written counterparts with lambda predicates */
template <typename F>
static bool all(const std::vector<int>& xs, F pred) {
for (int x : xs) {
if (!pred(x)) return false;
}
return true;
}
template <typename F>
static bool any(const std::vector<int>& xs, F pred) {
for (int x : xs) {
if (pred(x)) return true;
}
return false;
}
int main() {
std::vector<int> xs = {92, 47, 88};
std::cout << std::boolalpha;
std::cout << all(xs, [](int v) { return v > 40; }) << "\n";
std::cout << any(xs, [](int v) { return v < 50; }) << "\n";
for (size_t i = 0; i < xs.size(); i++) {
if (xs[i] > 80) { // first hit: value, then index
std::cout << xs[i] << "\n" << i << "\n";
break;
}
}
std::cout << xs.front() << "\n"; // array_first
std::cout << xs.back() << "\n"; // array_last
return 0;
}
true true 92 0 92 88
C# v11.0.100
// LINQ is the native counterpart: All/Any/First(predicate) mirror the
// PHP quartet, First()/Last() are array_first/array_last
var scores = new[]
{
KeyValuePair.Create("alice", 92),
KeyValuePair.Create("bob", 47),
KeyValuePair.Create("carol", 88),
};
Console.WriteLine(scores.All(p => p.Value > 40));
Console.WriteLine(scores.Any(p => p.Value < 50));
Console.WriteLine(scores.First(p => p.Value > 80).Value);
Console.WriteLine(scores.First(p => p.Value > 80).Key);
Console.WriteLine(scores[0].Value);
Console.WriteLine(scores[^1].Value);
True True 92 alice 92 88
Go v1.27.4
package main
import "fmt"
func main() {
// Go has no generics-based find in stdlib: hand-written loops
keys := []string{"alice", "bob", "carol"} // fixed order for determinism
scores := map[string]int{"alice": 92, "bob": 47, "carol": 88}
all, any := true, false
for _, v := range scores { // all/any results do not depend on order
if v <= 40 {
all = false
}
if v < 50 {
any = true
}
}
findVal, findKey := 0, ""
for _, k := range keys { // "first match" needs a fixed order
if scores[k] > 80 {
findVal, findKey = scores[k], k
break
}
}
fmt.Println(all)
fmt.Println(any)
fmt.Println(findVal)
fmt.Println(findKey)
fmt.Println(scores[keys[0]])
fmt.Println(scores[keys[len(keys)-1]])
}
true true 92 alice 92 88
Java v27.0.1
import java.util.LinkedHashMap;
import java.util.Map;
public class Main {
public static void main(String[] args) {
Map<String, Integer> scores = new LinkedHashMap<>();
scores.put("alice", 92);
scores.put("bob", 47);
scores.put("carol", 88);
System.out.println(scores.values().stream().allMatch(v -> v > 40));
System.out.println(scores.values().stream().anyMatch(v -> v < 50));
System.out.println(scores.values().stream().filter(v -> v > 80).findFirst().orElse(null));
System.out.println(scores.entrySet().stream().filter(e -> e.getValue() > 80).findFirst().map(Map.Entry::getKey).orElse(null));
var vals = scores.values().stream().toList(); // streams have no first/last
System.out.println(vals.get(0));
System.out.println(vals.get(vals.size() - 1));
}
}
true true 92 alice 92 88
JavaScript v26.13.1
// JS has the fullest native set: every/some/find/findIndex plus at()
const scores = { alice: 92, bob: 47, carol: 88 };
const vals = Object.values(scores);
console.log(vals.every((v) => v > 40));
console.log(vals.some((v) => v < 50));
console.log(vals.find((v) => v > 80));
console.log(vals.findIndex((v) => v > 80));
console.log(vals.at(0)); // array_first
console.log(vals.at(-1)); // array_last
true true 92 0 92 88
PHP v8.6.0
// PHP 8.4 added the callback quartet; 8.5 added array_first/array_last.
// Callbacks receive ($value, $key) and every call stops at the first hit.
$scores = ['alice' => 92, 'bob' => 47, 'carol' => 88];
var_dump(array_all($scores, fn($v) => $v > 40)); // every value passes
var_dump(array_any($scores, fn($v) => $v < 50)); // at least one passes
var_dump(array_find($scores, fn($v) => $v > 80)); // first matching value
var_dump(array_find_key($scores, fn($v) => $v > 80)); // first matching key
var_dump(array_first($scores)); // first value
var_dump(array_last($scores)); // last value
bool(true) bool(true) int(92) string(5) "alice" int(92) int(88)
Python v3.15.2
# Python: all()/any() are builtins; "find" is the next() idiom
scores = {"alice": 92, "bob": 47, "carol": 88}
print(all(v > 40 for v in scores.values()))
print(any(v < 50 for v in scores.values()))
print(next(v for v in scores.values() if v > 80)) # first matching value
print(next(k for k, v in scores.items() if v > 80)) # first matching key
vals = list(scores.values())
print(vals[0]) # first
print(vals[-1]) # last
True True 92 alice 92 88
Rust v1.99.1
fn main() {
let scores = [("alice", 92), ("bob", 47), ("carol", 88)];
println!("{}", scores.iter().all(|&(_, v)| v > 40));
println!("{}", scores.iter().any(|&(_, v)| v < 50));
println!("{}", scores.iter().find(|&&(_, v)| v > 80).map(|&(_, v)| v).unwrap());
println!("{}", scores.iter().find(|&&(_, v)| v > 80).map(|&(k, _)| k).unwrap());
println!("{}", scores.first().map(|&(_, v)| v).unwrap()); // array_first
println!("{}", scores.last().map(|&(_, v)| v).unwrap()); // array_last
}
true true 92 alice 92 88
TypeScript v26.13.1
// Same runtime as JS; the Record type makes the dictionary shape explicit
const scores: Record<string, number> = { alice: 92, bob: 47, carol: 88 };
const vals: number[] = Object.values(scores);
console.log(vals.every((v: number) => v > 40));
console.log(vals.some((v: number) => v < 50));
console.log(vals.find((v: number) => v > 80));
console.log(vals.findIndex((v: number) => v > 80));
console.log(vals.at(0));
console.log(vals.at(-1));
true true 92 0 92 88
Language differences
C: no built-in containers or higher-order predicates — the example hand-writes all/any/find with function pointers (the callback takes value and index, matching PHP's value+key); find returns the index, standing in for array_find_key.
C++: templates + lambdas hand-write the same loops; front/back on std::vector are array_first/last.
C#: LINQ is the native counterpart: All/Any/First(predicate) map one-to-one onto the PHP quartet, and First()/Last() are array_first/last; a KeyValuePair array keeps enumeration order deterministic.
Go: no generics-based find in stdlib — hand-written loops; note all/any results are order-independent (safe on a shuffling map), while "first match" needs a fixed key order, hence the keys slice.
Java: Stream.allMatch/anyMatch/filter(...).findFirst() cover the quartet; streams have no first/last, so the example materializes a List and indexes it.
JavaScript: the fullest native set: every/some/find/findIndex mirror the callback quartet and at(0)/at(-1) are array_first/last — exactly the direction PHP 8.4/8.5 followed.
PHP: 8.4 added array_all/array_any/array_find/array_find_key (callback signature callback(mixed $value, mixed $key): bool — both arguments are always passed, which makes internal functions like is_integer throw ArgumentCountError); 8.5 added array_first/array_last; all short-circuit at the first hit. array_find returns null when nothing matches — if null may be a legitimate value, the manual recommends array_find_key (null can't be a key, so no ambiguity).
Python: all()/any() are builtins taking generator expressions; there is no find function — the idiom is next((x for x in ... if ...), default), which raises StopIteration when absent (the sample data always matches).
Rust: Iterator::all/any/find are the native, lazily short-circuiting counterparts; first()/last() on the array literal cover the 8.5 pair.
TypeScript: same runtime as JS; the Record<string, number> annotation makes the dictionary shape explicit.