array_walk

array_walk(array &$array, callable $callback, mixed $arg = null): true

Applies a callback to every element in place (the callback may modify elements by reference).

C C23

C23
#include <stdio.h>

int main(void) {
    int xs[] = {1, 2, 3};
    for (int i = 0; i < 3; i++) {
        xs[i] *= 10;
    }
    printf("%d %d %d\n", xs[0], xs[1], xs[2]);
    return 0;
}
Output
10 20 30

C++ C++26

C++26
#include <algorithm>
#include <iostream>
#include <vector>

int main() {
    std::vector<int> xs{1, 2, 3};
    std::for_each(xs.begin(), xs.end(), [](int &v) { v *= 10; }); // a reference parameter mutates in place
    for (size_t i = 0; i < xs.size(); i++) {
        std::cout << xs[i] << (i + 1 < xs.size() ? " " : "\n");
    }
    return 0;
}
Output
10 20 30

C# v11.0.100

v11.0.100
var xs = new List<int> { 1, 2, 3 };
for (int i = 0; i < xs.Count; i++) {
    xs[i] *= 10; // List<T>.ForEach gets value copies; use an indexed loop to mutate elements
}
Console.WriteLine(string.Join(" ", xs));
Output
10 20 30

Go v1.27.4

v1.27.4
package main

import "fmt"

func main() {
	xs := []int{1, 2, 3}
	for i := range xs {
		xs[i] *= 10
	}
	fmt.Println(xs)
}
Output
[10 20 30]

Java v27.0.1

v27.0.1
import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        List<Integer> xs = new ArrayList<>(List.of(1, 2, 3));
        xs.replaceAll(v -> v * 10); // replaceAll swaps in place; forEach is read-only and cannot change elements
        System.out.println(xs);
    }
}
Output
[10, 20, 30]

JavaScript v26.13.1

v26.13.1
const xs = [1, 2, 3];
xs.forEach((v, i) => {
  xs[i] = v * 10; // the callback carries the index, so writing back mutates in place
});
console.log(xs);
Output
[ 10, 20, 30 ]

PHP v8.6.0

v8.6.0
$xs = [1, 2, 3];
array_walk($xs, function (&$v) { $v = $v * 10; });
echo json_encode($xs);
Output
[10,20,30]

Python v3.15.2

v3.15.2
xs = [1, 2, 3]
for i, v in enumerate(xs):
    xs[i] = v * 10  # no by-reference iteration in Python; write back by index
print(xs)
Output
[10, 20, 30]

Rust v1.99.1

v1.99.1
fn main() {
    let mut xs = vec![1, 2, 3];
    xs.iter_mut().for_each(|v| *v *= 10); // iter_mut yields mutable borrows
    println!("{:?}", xs);
}
Output
[10, 20, 30]

TypeScript v26.13.1

v26.13.1
const xs: number[] = [1, 2, 3];
xs.forEach((v, i) => {
  xs[i] = v * 10; // the callback carries the index, so writing back mutates in place
});
console.log(xs);
Output
[ 10, 20, 30 ]

Language differences

C: no higher-order functions — a hand-written for loop mutates in place (example).

C++: std::for_each with a reference-taking lambda mutates in place (example); the ranges version is std::ranges::for_each.

C#: the delegate in List<T>.ForEach receives a copy (value types cannot touch the original) — use an indexed for loop for in-place mutation (example); true reference semantics need a wrapper class.

Go: no higher-order array traversal; write back by index with for i := range (example); note for _, v := range gives v as a copy.

Java: List.replaceAll(UnaryOperator) is the right entry point for in-place replacement (example); a forEach lambda only sees element values and cannot modify the list.

JavaScript: the forEach callback's second parameter is the index, so writing xs[i] back mutates in place (example); semantically it is the closest to array_walk's in-place + key features.

PHP: the array_walk callback signature function (&$v, $k) receives the element by reference and optionally the key, with a third userdata parameter for extra context; versus array_map it mutates in place, returns true, never re-keys, and can be interrupted.

Python: binding semantics rule out by-reference iteration — for i, v in enumerate(xs) writes back by index (example); the comprehension xs = [v * 10 for v in xs] builds a new list instead.

Rust: iter_mut() yields a mutable-borrowing iterator and for_each dereferences to modify (example); iter() is read-only.

TypeScript: same forEach write-back pattern as JavaScript (example); the type system guarantees written elements match the element type.