Cutting down on lines of code with array_walk()

Hello there!

Today I am going to be talking about the array_walk php function which can help you cut down on lines of code and organize your code better.

An easy way to think of array_walk() is think of it as an automatic way to loop through arrays of data. From time to time in your coding duties there are likely to be occasions when you find yourself writing these kind of loops over and over again:

$my_array = array( 'dog', 'cat', 'rabbit', 'bird' );

foreach( $my_array as $animal ){

process_animal( $animal );

The above routine can be reflected with less lines of code using the array_walk() function:

$my_array = array( 'dog', 'cat', 'rabbit', 'bird' );

array_walk( $my_array, 'process_animal' );

As you can see above, there is quite a reduction in code happening here. To find out more about array_walk please look at the PHP manual excerpt here.


Sign up today to stay informed with industry news & trends