DEV Community

Gurpreet Kait
Gurpreet Kait

Posted on • Originally published at larachamp.com on

Array Map Function In PHP

Array map function contains very amazing use cases. I was reading somewhere about it and today I used it somewhere in my personal project. I thought that let’s share a few interesting use cases of array_map() function.

Basically, It’s a way to transform the elements of an array using custom logic without modifying the original array .

The basic syntax of array_map() a function looks like this:

array_map(callback_function, array1, array2, ...);
Enter fullscreen mode Exit fullscreen mode

array map function in php

Callback_function: It can be a manually written php function or you can also use a built-in php function to manipulate each item of an array. So, It’s more useful we don’t need to use foreach() it all the time. It will be helpful to refactor the extra code where we may be using foreach or something else.

For Example:

If we want to make each element of an array to the upper case then we can directly use the array_map function with the built-in function ‘strtoupper’ in its first argument,m instead of doing it with foreach.

Converting Values

As you know the first parameter takes the callback function and it could be a built-in function or custom. In the below code, we have used strtoupper a function to make each value in uppercase. This is how we can convert values from a single array. It will not work with multiple arrays at the same time.

see:

$array = ['Robin','Rasud','Aarvi','Anmol'];

$res = array_map('strtoupper',$array);

print_r($res);
// Output: Array ( [0] => ROBIN [1] => RASUD [2] => AARVI [3] => ANMOL )
Enter fullscreen mode Exit fullscreen mode

And it has the option to pass multiple arrays in the last argument place.

...arrays

Combining Arrays

By using array_map() function we can combine multiple arrays. See:

$array = ['Robin','Rasud','Aarvi','Anmol'];
$array2 = ['cham','Guri','Aarju','Manpreet'];
$array3 = ['amir','sidhu','saka','Gullu'];

$res = array_map(function($value,$secArr,$prop){
        echo $value. ' '. $secArr .' '. $prop. '<br>';
},$array,$array2,$array3);

// Robin cham amir
// Rasud Guri sidhu
// Aarvi Aarju saka
// Anmol Manpreet Gullu
Enter fullscreen mode Exit fullscreen mode

Using With Objects

As I have tried it with objects, It smoothly works with objects as well. I didn’t expect that it will be working with an object. Let’s see how I did this and tried on my side:

class car {
    public $name;
    public function __construct(
        $name
    ){
        $this->name = $name;
    }
}
$carNames = [new car('Buggati'), new car('Mercedes'), new car('BMW')];
$res = array_map(function($value){
        echo 'You gonna have -> '. $value->name . '<br>';
},$carNames);

//output
You gonna have -> Buggati
You gonna have -> Mercedes
You gonna have -> BMW
Enter fullscreen mode Exit fullscreen mode

Conclusion

The array_map() the function gives us the flexibility to work with multiple arrays without having to worry about foreach() or any other loop. Why I’m repeating foreach again and again because we usually use foreach for array tasks but we don’t need foreach each time we play with arrays. So, don’t we worry about this now? Have fun now.

related: Collections In Laravel

External: array_map() function

Remember that the array_map function does not modify the original array; it creates a new array containing the results of applying the callback to each element. Also, make sure that the arrays you pass array_map have the same length or unexpected behavior may occur.

The post Array Map Function In PHP appeared first on Larachamp.

Top comments (0)