Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / PHP

array_walk()

Visit each array element with a callback receiving the value and key, plus an optional user argument.

Version: PHP 8.4; repository runtime, production runtime not attested

Syntax

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

Visit each array element with a callback receiving the value and key, plus an optional user argument. A callback taking the value by reference can change it in place. Its return value is ignored. Keep the collection structure intact during traversal; use this operation for modifying existing values.

Signature

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

Example output

{"a":6,"b":12}

Examples

<?php
$prices = ['a'=>2,'b'=>4];
array_walk($prices, function (&$price, $key, $factor) { $price *= $factor; }, 3);
echo json_encode($prices);

Result

{"a":6,"b":12}

Sources