Menu
Coddy logo textTech

Walking with 'array_walk'

Part of the Logic & Flow section of Coddy's PHP journey — lesson 44 of 68.

The array_walk() function offers a different approach to processing arrays compared to array_map(). While array_map() creates a new array with transformed values, array_walk() modifies the original array directly by applying a callback function to each element in-place.

This function is particularly useful when you want to change the existing array without creating a copy. The callback function you provide receives each array element by reference, allowing you to modify the original values directly.

<?php
function addPrefix(&$value, $key) {
    $value = "Item: " . $value;
}

$items = ["apple", "banana", "orange"];
array_walk($items, "addPrefix");
// $items is now: ["Item: apple", "Item: banana", "Item: orange"]
?>

Notice how the callback function uses &$value to receive the array element by reference. This allows the function to modify the original array element directly. The second parameter $key provides access to the current element's key, though it's not always needed for simple transformations.

array_walk() also accepts an optional third argument — extra data that gets passed directly to your callback as a third parameter. This is useful when your transformation depends on a value defined outside the array.


<?php
function addPrefix(&$value, $key, $prefix) {
    $value = $prefix . $value;
}

$items = ["apple", "banana", "orange"];
array_walk($items, "addPrefix", "Item: ");
// $items is now: ["Item: apple", "Item: banana", "Item: orange"]
?>

Here, the string "Item: " is passed as the third argument to array_walk() and received as $prefix inside the callback. This lets you reuse the same callback with different extra values.
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

challenge icon

Challenge

Easy

You will receive two inputs: a comma-separated list of product names and a category prefix. The first input contains product names as a string in the format laptop,mouse,keyboard,monitor, and the second input is a category prefix text (example: Electronics).

Read both inputs, convert the comma-separated product names into an array, create a callback function that adds the category prefix to each product name in the format "Category: ProductName", use array_walk() with your callback function to modify the original array directly, and print each modified product name on a separate line.

Your callback function must use the &$value parameter to modify array elements by reference. The function should concatenate the category prefix with a colon and space, followed by the product name.

Input format:

  • First line: Comma-separated product names (example: laptop,mouse,keyboard,monitor)
  • Second line: Category prefix text (example: Electronics)

Expected output: Each modified product name printed on a separate line in the format Category: ProductName

Cheat sheet

The array_walk() function modifies the original array directly by applying a callback function to each element in-place, unlike array_map() which creates a new array.

The callback function receives each array element by reference using &$value, allowing direct modification of the original values. The second parameter $key provides access to the current element's key.

array_walk() also accepts an optional third argument — extra data that gets passed to the callback as a third parameter. This is useful when your callback needs additional information to process each element.

<?php
function addPrefix(&$value, $key) {
    $value = "Item: " . $value;
}

$items = ["apple", "banana", "orange"];
array_walk($items, "addPrefix");
// $items is now: ["Item: apple", "Item: banana", "Item: orange"]

// Using the optional third argument (extra data):
function applyPrefix(&$value, $key, $prefix) {
    $value = $prefix . $value;
}

$fruits = ["apple", "banana", "orange"];
array_walk($fruits, "applyPrefix", "Fresh: ");
// $fruits is now: ["Fresh: apple", "Fresh: banana", "Fresh: orange"]
?>

Try it yourself

<?php
// Read the comma-separated product names
$products_input = trim(fgets(STDIN));

// Read the category prefix
$category = trim(fgets(STDIN));

// Convert the comma-separated string into an array
$products = explode(',', $products_input);

// TODO: Write your code below
// Create a callback function that modifies each product name by adding the category prefix
// Use array_walk() with your callback function to modify the array


// Output each modified product name on a separate line
foreach ($products as $product) {
    echo $product . "\n";
}
?>
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Logic & Flow