PHP Modify Array Elements with array_walk() Function

The array_walk() function in PHP is used to iterate over an array and apply a user-defined callback function to each element. This allows you to modify the array's values without having to manually loop through it. To modify the array elements, you need to pass the callback function by reference.

Passing array values by reference to array_walk() modifies the original array. Often, this is what you want. However, if you want to preserve the original array, consider using array_map().

Example 1: Doubling Product Prices

 
$products = [
    "laptop" => 1200,
    "smartphone" => 600,
    "tablet" => 400,
];
 
function doublePrice(&$price, $key) {
    $price *= 2;
}
 
array_walk($products, "doublePrice");
 
print_r($products);
 
 

This example doubles the price of each product in the products array. The doublePrice() function takes the product price and its key as arguments and modifies the price directly. The & before $price indicates that the price should be passed by reference, allowing the function to modify the original array element.

Example 2: Adding Prefix to Vehicle Names

 
$vehicles = [
    "car" => "Toyota Camry",
    "truck" => "Ford F-150",
    "motorcycle" => "Honda CBR650R",
];
 
function addPrefix(&$vehicleName, $key) {
    $vehicleName = "Brand: " . $vehicleName;
}
 
array_walk($vehicles, "addPrefix");
 
print_r($vehicles);
 
 

This example adds the prefix "Brand: " to the name of each vehicle in the vehicles array. The addPrefix() function takes the vehicle name and its key as arguments and modifies the vehicle name directly. The & before $vehicleName indicates that the vehicle name should be passed by reference, allowing the function to modify the original array element.

Example 3: Converting Ship Capacity to Metric Tons

 
$ships = [
    "Evergreen" => 200000,
    "Maersk Mc-Kinney Moller" => 180000,
    "COSCO Beijing" => 190000,
];
 
function convertToMetricTons(&$capacity, $key) {
    $capacity *= 0.907185;
}
 
array_walk($ships, "convertToMetricTons");
 
print_r($ships);
 
 

This example converts the ship capacity from imperial tons to metric tons. The convertToMetricTons() function takes the ship capacity and its key as arguments and modifies the capacity directly. The & before $capacity indicates that the capacity should be passed by reference, allowing the function to modify the original array element.

Example 4: Calculating Order Discounts

 
$orders = [
    1 => [
        "items" => ["laptop" => 1, "smartphone" => 1],
        "total" => 1800,
    ],
    2 => [
        "items" => ["tablet" => 2, "mouse" => 1],
        "total" => 800,
    ],
];
 
function applyDiscount(&$order) {
    $discount = 0.1;
    $order["total"] *= (1 - $discount);
}
 
array_walk($orders, "applyDiscount");
 
print_r($orders);
 
 

This example applies a 10% discount to the total amount of each order in the orders array. The applyDiscount() function takes the order information as an argument and modifies the total property directly.