array map key value php code example

Example 1: using array map php

array_map ( callable $callback , array $array1 [, array $... ] ) : array

Example 2: php map array

function cube($number)
{
    return ($number * $number * $number);
}

$nums = [1, 2, 3, 4, 5];
$cubed = array_map('cube', $nums);

Example 3: php array map

$func = function cube($n) {
    return ($n * $n * $n);
}
$a = [1, 2, 3, 4, 5];
$b = array_map( $func, $a );

// Outputs: Array (
//    [0] => 2
//    [1] => 4
//    [2] => 6
//    [3] => 8
//    [4] => 10
// )

Tags:

Php Example