• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / PHP / php array_walk() – Modify Elements in Array with Callback Function

php array_walk() – Modify Elements in Array with Callback Function

April 19, 2022 Leave a Comment

The php array_walk() function gives us the ability to apply a function to all elements in an array. array_walk() modifies the passed array if the function parameter is passed by reference

function square(&$var) {
    $var =  $var * $var;
}

$array = array(1,2,3,4,5);

array_walk($array, "square");

print_r($array);

// Output:
Array
(
    [0] => 1
    [1] => 4
    [2] => 9
    [3] => 16
    [4] => 25
)

When working with collections of data in php, the ability to easily access and manipulate the elements of arrays.

One such operation is being able to apply a function to each element of an array.

The php array_walk() function gives us the ability to apply functions to arrays. The callback function will be applied to each element of an array, and the array will be modified in place.

Below is a basic example showing how you can use array_walk() to square each element of an array of numbers in php.

function square(&$var) {
    $var =  $var * $var;
}

$array = array(1,2,3,4,5);

array_walk($array, "square");

print_r($array);

// Output:
Array
(
    [0] => 1
    [1] => 4
    [2] => 9
    [3] => 16
    [4] => 25
)

You can also define and pass the callback function directly to array_walk().

Below is another example of how you can define and pass a callback function to array_walk().

$array = array(1,2,3,4,5);

array_walk($array, function(&$var) {
    return $var * $var;
});

print_r($array);

// Output:
Array
(
    [0] => 1
    [1] => 4
    [2] => 9
    [3] => 16
    [4] => 25
)

Passing a Third Parameter to the Callback Function with array_walk()

The php array_walk() function has two required parameters; the array to work on and a callback function.

There is a third optional parameter which can be passed to the callback function.

For example, let’s say we have an array of numbers and want to multiply all of them by another number.

We can do that with array_walk() by utilizing this third parameter.

Below is an example of how to use array_walk() to pass an argument to the callback function.

function multiply(&$val, $key, $num) {
    $val =  $val * $num;
}

$array = array(1,2,3,4,5);

array_walk($array, "multiply", 5);

print_r($array);

// Output:
Array
(
    [0] => 5
    [1] => 10
    [2] => 15
    [3] => 20
    [4] => 25
)

The Difference between array_map() and array_walk() in php

When I was learning about array_walk() and the php array_map() function for the first time, I was confused since it seemed to me like they did the same thing.

There are a few differences between array_map() and array_walk().

The main difference is that array_walk() operates on only one array and does not return a new array. This is different from array_map() as we can pass multiple arrays to array_map()

and create a new array.

Depending on the situation, it may be more beneficial to use one function over the other. But in general, array_walk() is more resource efficient when working with one array.

Hopefully this article has been useful for you to learn how to use the php array_walk() function to apply functions to arrays in php.

Other Articles You'll Also Like:

  • 1.  Check if Variable Exists in php with isset() Function
  • 2.  php Convert Degrees to Radians with php deg2rad() Function
  • 3.  Get Days in Month Using php
  • 4.  php ceil – Round Up and Find the Ceiling of a Number in php
  • 5.  php acosh – Find Hyperbolic Arccosine of Number Using acosh() Function
  • 6.  Check if String Starts with Given String in php with str_starts_with()
  • 7.  How to Check If String Contains Substring in PHP
  • 8.  php property_exists() – Check if Property Exists in Class or Object
  • 9.  php array_merge() – Merge One or More Arrays Together by Appending
  • 10.  Get User Agent in php with $_SERVER[‘HTTP_USER_AGENT’]

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy