• 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
  • VBA
  • 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.  Get First Element of Array in php
  • 2.  echo php – Output One or More Strings
  • 3.  php in_array – Check If Value is in php Array
  • 4.  php str_split() – Convert String into Array of Equal Length Substrings
  • 5.  php Rename File – Renaming Files and Directories Easily
  • 6.  php Convert Radians to Degrees with php rad2deg() Function
  • 7.  php is_array() Function – Check if Variable is Array in php
  • 8.  php acos – Find Arccosine and Inverse Cosine of Number
  • 9.  Get Word Count of String in php with str_word_count() Function
  • 10.  php is_string() Function – Check if Variable is String in php

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

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

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 © 2022 · The Programming Expert · About · Privacy Policy