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

The Programming Expert

Solving All of Your Programming Headaches

  • Home
  • Learn to Code
    • Python
    • JavaScript
  • Code Snippets
    • HTML
    • JavaScript
    • jQuery
    • PHP
    • Python
    • SAS
    • Ruby
  • About
You are here: Home / PHP / php array_map() – Create New Array from Callback Function

php array_map() – Create New Array from Callback Function

April 13, 2022 Leave a Comment

The php array_map() function gives us the ability to create a new array by applying a function to all elements of one or more arrays.

function square($var) {
    return $var * $var;
}

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

print_r(array_map("square", $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 manipulate the elements of arrays.

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

The php array_map() function gives us the ability to create new arrays by applying a function to one or more arrays. The callback function will be applied to each element of the input arrays.

Below is a basic example showing how you can use array_map() to create a new array which has the squares of each element of a given array of numbers in php.

function square($var) {
    return $var * $var;
}

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

print_r(array_map("square", $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_map().

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

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

print_r(array_map(function($var) {
    return $var * $var;
},$array));

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

Using a lambda Function as the Callback Function with array_map() in php

When using array_map(), you can pass a lambda function as the callback function.

Let’s rewrite our square function as a lambda function and pass it to array_map().

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

print_r(array_map(fn($var): float => $var * $var,$array));

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

Applying a Function to Multiple Arrays with array_map() in php

We can use array_map() and apply a function which will create a new array from more than one array.

Up until this point in the article, we’ve only been working with one input array, but let’s define a function which will operate on two arrays.

One example of this is if we have two arrays and we want to perform element-wise multiplication.

Below is an example of how to do element-wise multiplication with array_map() in php.

$array1 = array(1,2,3,4,5);
$array2 = array(6,7,8,9,10);

print_r(array_map(function($v1,$v2) {
    return $v1 * $v2;
},$array1,$array2));

// Output:
Array
(
    [0] => 6
    [1] => 14
    [2] => 24
    [3] => 36
    [4] => 50
)

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

Other Articles You'll Also Like:

  • 1.  php Rename File – Renaming Files and Directories Easily
  • 2.  php array_intersect() – Find Intersection of Two or More Arrays in php
  • 3.  php ceil – Round Up and Find the Ceiling of a Number in php
  • 4.  php min – Find Minimum Value of Array in php
  • 5.  Get Last Element of Array in php
  • 6.  php max – Find Maximum Value of Array in php
  • 7.  php asin – Find Arcsine and Inverse Sine of Number Using asin() Function
  • 8.  Check if Variable Exists in php with isset() Function
  • 9.  php sleep() Function – Pause Execution of Code in php
  • 10.  php Move File from One Directory to Another Folder

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