Difference between array_map, array_walk and array_filter

Matheus Mello
Matheus Mello
September 2, 2023
Cover Image for Difference between array_map, array_walk and array_filter

Demystifying array_map, array_walk, and array_filter in PHP

💡 Are you confused🤯 about the differences between array_map, array_walk, and array_filter in PHP? 🤔 Don't worry, my friend, I've got you covered! Let's dive into the magical world of arrays and callbacks 🌟

Understanding the basics

⚡ First things first, let's understand what each function does:

1. array_map:

array_map is a function that applies a callback function to each element of an array and returns a new array containing the modified values⚡. It works by iterating over each element of the input array and passing the element as an argument to the callback function🔜. You can then perform any desired operation on each element and the modified value will be stored in the resulting array.

2. array_walk:

On the other hand, array_walk is a function that applies a callback function to each element of an array in place🔄. This means that it modifies the original array directly, without creating a separate copy. The callback function receives both the value and the key of the current array element as arguments and can modify the element or perform any desired operation on it💪.

3. array_filter:

Meanwhile, array_filter is a function that filters an array by applying a callback function to each element. It returns a new array containing only the elements for which the callback function returns true🔍. The callback function should return either true or false based on a condition that you define. This is useful for selectively extracting specific elements from an array based on your criteria.

How are they different?

🔁 To put it simply:

  • array_map modifies and returns a new array.

  • array_walk modifies the original array directly.

  • array_filter returns a new array containing only the filtered elements.

✨ Let's illustrate these differences with some examples:

$numbers = [1, 2, 3, 4, 5];

// Example 1: array_map
$squaredNumbers = array_map(function($num) {
    return $num * $num;
}, $numbers);

print_r($squaredNumbers);
// Output: [1, 4, 9, 16, 25]

// Example 2: array_walk
$sum = 0;
array_walk($numbers, function($num) use (&$sum) {
    $sum += $num;
});

echo $sum;
// Output: 15

// Example 3: array_filter
$evenNumbers = array_filter($numbers, function($num) {
    return $num % 2 === 0;
});

print_r($evenNumbers);
// Output: [2, 4]

🎉 Cool, right? By understanding the differences between these functions, you can now use them effectively in your code and save yourself from potential confusion.

⚡ So, to answer your questions:

💡 No, array_map, array_walk, and array_filter do not perform the same thing. Each function has its unique purpose, which we just explored.

💡 While there might be some scenarios where you could use them interchangeably, it's essential to understand the differences and choose the one that best fits your specific use case.

🚀 Now it's your turn! Go ahead and experiment with these functions and have fun unleashing their full potential in your PHP code!

🎁 If you found this guide helpful, please consider sharing it with your fellow coders and spread the knowledge! 💙

✉️ I would love to hear your thoughts and answer any further questions you might have. Drop a comment below or reach out to me on Twitter. Let's code together and conquer the PHP universe! 🌌

Take Your Tech Career to the Next Level

Our application tracking tool helps you manage your job search effectively. Stay organized, track your progress, and land your dream tech job faster.

Your Product
Product promotion

Share this article

More Articles You Might Like

Latest Articles

Cover Image for How can I echo a newline in a batch file?
batch-filenewlinewindows

How can I echo a newline in a batch file?

Published on March 20, 2060

🔥 💻 🆒 Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Cover Image for How do I run Redis on Windows?
rediswindows

How do I run Redis on Windows?

Published on March 19, 2060

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! 🚀 Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Cover Image for Best way to strip punctuation from a string
punctuationpythonstring

Best way to strip punctuation from a string

Published on November 1, 2057

# The Art of Stripping Punctuation: Simplifying Your Strings 💥✂️ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Cover Image for Purge or recreate a Ruby on Rails database
rakeruby-on-railsruby-on-rails-3

Purge or recreate a Ruby on Rails database

Published on November 27, 2032

# Purge or Recreate a Ruby on Rails Database: A Simple Guide 🚀 So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? 🤔 Well, my