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 supports an optional third argument that lets you pass extra data directly to your callback function. This is useful when your transformation depends on a value defined outside the array — such as a prefix, suffix, or any other dynamic input.

When you pass a third argument to array_walk(), your callback function receives it as a third parameter:

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

$products = ["laptop", "mouse", "keyboard"];
$prefix = "Electronics";
array_walk($products, "addCategory", $prefix);
// $products is now: ["Electronics: laptop", "Electronics: mouse", "Electronics: keyboard"]
?>

Here, $prefix is passed as the third argument to array_walk(), and the callback receives it as $category. This way, you can reuse the same callback function with different prefixes without hardcoding any values inside it.

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

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