array_mapAPPLIES the callback to the elements of the given ARRAYS.
This function returns an ARRAYS containing the results of applying the $callback to the corresponding index of $array1 and $array2, ..., $arrayN if provided, used as arguments for $callback.
The number of parameters that the $callback accepts should match the number of arrays passed to this function.
NULL can be passed to $callback to perform a zip operation on multiple arrays.
If only $array1 is provided, this function will return the input ARRAY.
<?php
arr array_map ( callable $callback , arr $array1 [,
arr $array2, ..., $arrayN ] )
where,
$callback = The callback function
$array1 = An ARRAY to run through the callback function
$array2 = The second ARRAY to run through the callback function
. . . . . . . . . . .
$arrayN = The last ARRAY to run through the callback function
?>
$callback
The callback function to run for each element in each ARRAY.
$array1
The first ARRAY to run through the callback function.
$array2
The second ARRAY to run through the callback function.
$arrayN
The last ARRAY to run through the callback function.
EXERCISE
<?php
function pairname01 ($i, $j, $k)
{
return $j . ' or ' . $k;
}
$nbrduarfs = [ 1, 2, 3, 4, 5, 6, 7 ];
$actduarfs = [ 'Sneezy', 'Sleepy', 'Dopey',
'Doc', 'Happy', 'Bashful', 'Grumpy' ];
$argduarfs = [ 'Blick', 'Flick', 'Glick',
'Snick', 'Plick', 'Whick', 'Quee' ];
echo 'FIRST ARRAY:<br><pre>';
print_r($nbrduarfs);
echo '</pre><br><br>';
echo 'SECOND ARRAY:<br><pre>';
print_r($actduarfs);
echo '</pre><br><br>';
echo 'THIRD ARRAY:<br><pre>';
print_r($argduarfs);
echo '</pre><br><br>';
echo 'ARRAY after array_map:<br><br>';
$arrresult = array_map ('pairname01', $nbrduarfs,
$actduarfs, $argduarfs);
foreach($arrresult as $arrr)
{
echo $arrr . '<br>';
}
?>
RESULT
FIRST ARRAY:
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 6
[6] => 7
)
SECOND ARRAY:
Array
(
[0] => Sneezy
[1] => Sleepy
[2] => Dopey
[3] => Doc
[4] => Happy
[5] => Bashful
[6] => Grumpy
)
THIRD ARRAY:
Array
(
[0] => Blick
[1] => Flick
[2] => Glick
[3] => Snick
[4] => Plick
[5] => Whick
[6] => Quee
)
ARRAY after array_map
According the callback function: pairname01
Sneezy or Blick
Sleepy or Flick
Dopey or Glick
Doc or Snick
Happy or Plick
Bashful or Whick
Grumpy or Quee
EXERCISE
<?php
function cnvrthex02 ($dec02)
{
return (strtoupper(dechex($dec02)));
}
$vlrs02 = array(1500 => 1500, 1808 => 1808,
1889 => 1889, 1945 => 1945,
idate('Y') => idate('Y'));
echo 'GIVEN ARRAY<br><pre>';
print_r($vlrs02);
echo '</pre><br><br>ARRAY after array_map<br>';
$arrrs02 = array_map ('cnvrthex02', $vlrs02);
foreach($arrrs02 as $ar02 => $arr02)
{
echo '( ' . $ar02 . ' )<sub>D</sub> =
( ' . $arr02 . ' )<sub>H</sub><br>';
}
?>
EXERCISE
<?php
$arr03a = [ 'one', 'two', 'three', 'four' ];
$arr03b = [ 'five', 'six', 'seven' ];
$zipdarr03ab = array_map(NULL, $arr03a, $arr03b);
echo 'FISRT ARRAY ELEMENTS:<br>';
foreach($arr03a as $arr03ap => $arr03app)
{
var_dump($arr03ap);
echo ' = ';
var_dump($arr03app);
echo '<br><br>';
}
echo '<br><br>SECOND ARRAY ELEMENTS:<br>';
foreach($arr03b as $arr03bp => $arr03bpp)
{
var_dump($arr03bp);
echo ' = ';
var_dump($arr03bpp);
echo '<br><br>';
}
echo '<br><br>ZIPPED ARRAY ELEMENTS:<br>';
foreach($zipdarr03ab as $zp03 => $zp03p)
{
var_dump($zp03);
echo ' = ';
var_dump($zp03p);
echo '<br><br>';
}
?>
EXERCISE
<?php
function remainder04 ($x04, $y04)
{
return $x04%$y04;
}
$arr04a = [ 1, 2, 3, 4 ];
$arr04b = [ 5, 6, 7, 8 ];
$zipdarr04ab = array_map('remainder04', $arr04a, $arr04b);
echo 'FISRT ARRAY ELEMENTS:<br>';
foreach($arr04a as $arr04ap => $arr04app)
{
var_dump($arr04ap);
echo ' = ';
var_dump($arr04app);
echo '<br><br>';
}
echo '<br><br>SECOND ARRAY ELEMENTS:<br>';
foreach($arr04b as $arr04bp => $arr04bpp)
{
var_dump($arr04bp);
echo ' = ';
var_dump($arr04bpp);
echo '<br><br>';
}
echo '<br><br>RESULTING ARRAY ELEMENTS:<br>';
foreach($zipdarr04ab as $zp04 => $zp04p)
{
var_dump($zp04);
echo ' = ';
var_dump($zp04p);
echo '<br><br>';
}
?>
EXERCISE
<?php
function remainder05 ($x05, $y05)
{
return $x05%$y05;
}
$arr05a = [ 'one' => 1, 'two' => 2, 'three' => 3, 'four' => 4 ];
$arr05b = [ 'five' => 5, 'six' => 6, 'seven' => 7, 'eigth' => 8 ];
$zipdarr05ab = array_map('remainder05', $arr05a, $arr05b);
echo 'FISRT ARRAY ELEMENTS:<br>';
foreach($arr05a as $arr05ap => $arr05app)
{
var_dump($arr05ap);
echo ' = ';
var_dump($arr05app);
echo '<br><br>';
}
echo '<br><br>SECOND ARRAY ELEMENTS:<br>';
foreach($arr05b as $arr05bp => $arr05bpp)
{
var_dump($arr05bp);
echo ' = ';
var_dump($arr05bpp);
echo '<br><br>';
}
echo '<br><br>RESULTING ARRAY ELEMENTS:<br>';
foreach($zipdarr05ab as $zp05 => $zp05p)
{
var_dump($zp05);
echo ' = ';
var_dump($zp05p);
echo '<br><br>';
}
?>
EXERCISE
<?php
echo "Testing array_map() : basic functionality.<br>";
function multiply($p, $q) {
return ($p * $q);
}
function square($p) {
return ($p * $p);
}
function concatenate_($a, $b) {
return ("$a = $b");
}
// integer array
$arr1 = array(1, 2, 3);
$arr2 = array(4, 5, 6);
echo 'The given $arr1 (integer):<br>';
var_dump($arr1);
echo 'The given $arr2 (integer):<br>';
var_dump($arr2);
echo "<br><br>With two integer array.<br>";
echo 'After array_map(\'multiply\', $arr1, $arr2):<br>';
var_dump( array_map('multiply', $arr1, $arr2) );
echo "<br><br>With two integer array.<br>";
echo 'After array_map(\'square\', $arr1):<br>';
var_dump( array_map('square', $arr1) );
// string array
$arr1 = array("one", "two");
$arr2 = array("single", "double");
echo '<br><br>The given $arr1 (string):<br>';
var_dump($arr1);
echo 'The given $arr2 (string):<br>';
var_dump($arr2);
echo "<br><br>With string array.<br>";
echo 'After array_map(\'concatenate_\', $arr1, $arr2):<br>';
var_dump( array_map('concatenate_', $arr1, $arr2) );
?>
EXERCISE
<?php
echo "Testing array_map() : string keys.<br><br>";
$arr = array("stringkey" => "value");
echo 'The given $arr:<br>';
var_dump($arr);
function cb1 ($a) {return array ($a);};
function cb2 ($a,$b) {return array ($a,$b);};
echo '<br><br>After array_map("cb1", $arr):<br>';
var_dump( array_map("cb1", $arr));
echo '<br><br>After array_map("cb2", $arr):<br>';
var_dump( array_map("cb2", $arr,$arr));
echo '<br><br>After array_map(null, $arr):<br>';
var_dump( array_map(null, $arr));
echo '<br><br>After array_map(null, $arr, $arr):<br>';
var_dump( array_map(null, $arr, $arr));
?>
EXERCISE
<?php
echo "Testing array_map() : references.<br><br>";
$arr = array("k1" => "v1","k2"=>"v2");
echo 'The given $arr:<br>';
var_dump($arr);
$arr[]=&$arr["k1"];
$arr[]=&$arr;
function cb1 ($a) {var_dump ($a);return array ($a);};
echo '<br><br>After array_map("cb1", $arr):<br>';
var_dump( array_map("cb1", $arr));
echo '<br><br>After array_map(null, $arr):<br>';
var_dump( array_map(null, $arr));
echo '<br><br>After array_map(null, $arr, $arr):<br>';
var_dump( array_map(null, $arr, $arr));
// break cycles
$arr[0] = null;
$arr[1] = null;
?>
EXERCISE
<?php
/*
* Test array_map() by passing
* different arrays for $arr1 argument
*/
echo "Testing array_map() : different arrays
for 'arr1' argument.<br><br>";
function callback($a)
{
return ($a);
}
// different arrays
$arrays = [
/*1*/ [ 1, 2 ],
// array with default keys and numeric values
[ 1.1, 2.2 ],
// array with default keys & float values
[ [2], [1] ],
// sub arrays
[ false,true ],
// array with default keys and boolean values
[],
// empty array
[ NULL ],
// array with NULL
[ "a","aaaa","b","bbbb","c","ccccc" ],
// associative arrays
/*8*/ [ 1 => "one", 2 => "two", 3 => "three" ],
// explicit numeric keys, string values
[ "one" => 1, "two" => 2, "three" => 3 ],
// string keys & numeric values
[ 1 => 10, 2 => 20, 4 => 40, 3 => 30 ],
// explicit numeric keys and numeric values
[ "one" => "ten", "two" => "twenty", "three" => "thirty" ],
// string key/value
[ "one" => 1, 2 => "two", 4 => "four" ],
//mixed
// associative array, containing
// null/empty/boolean values as key/value
/*13*/ [ NULL => "NULL", null => "null",
"NULL" => NULL, "null" => null ],
[ true => "true", false => "false",
"false" => false, "true" => true ],
[ "" => "emptyd", '' => 'emptys',
"emptyd" => "", 'emptys' => '' ],
[ 1 => '', 2 => "", 3 => NULL, 4 => null,
5 => false, 6 => true ],
[ '' => 1, "" => 2, NULL => 3,
null => 4, false => 5, true => 6 ],
// array with repetitive keys
/*18*/ [ "One" => 1, "two" => 2, "One" => 10,
"two" => 20, "three" => 3 ]
];
echo 'The given $arrays:<br><pre>';
var_dump($arrays);
echo '</pre>';
// loop through the various elements
// of $arrays to test array_map()
$iterator = 1;
foreach($arrays as $arr1) {
echo "<br><br><b>Iteration: $iterator</b><br>";
var_dump( array_map('callback', $arr1) );
$iterator++;
}
?>
EXERCISE
<?php
/*
* Test array_map() by passing array
* having different subarrays
*/
echo "Testing array_map() : array having subarrays.<br><br>";
function callback($a)
{
return $a;
}
// different subarrays
$arr1 = array(
array(),
array(1, 2),
array('a', 'b'),
array(1, 2, 'a', 'b'),
array(1 => 'a', 'b' => 2)
);
echo 'The given $arr1:<br>';
var_dump($arr1);
echo '<br><br>After array_map(\'callback\', $arr1):<br>';
var_dump( array_map('callback', $arr1));
?>
EXERCISE
<?php
/*
* Test array_map() by passing array having different size
* 1) first array as empty array
* 2) second array as empty array
* 3) second array shorter than first array
* 4) first array shorter than second array
* 5) one more array than callback function arguments
*/
echo "Testing array_map() : arrays with different size.<br><br>";
function callback($a, $b)
{
return array($a => $b);
}
// calling array_map with different arrays
var_dump( array_map('callback',
array(1, 2, 3), array()) );
echo '<br><br>';
var_dump( array_map('callback',
array(), array('a', 'b', 'c')) );
echo '<br><br>';
var_dump( array_map('callback',
array(1, 2, 3), array('a', 'b')) );
echo '<br><br>';
var_dump( array_map('callback',
array(012, 0x2F, 0X1A),
array(2.3, 12.4e2)) );
echo '<br><br>';
var_dump( array_map('callback',
array(), array(1, 2, 3),
array('a', 'b')) );
// passing more number of arrays than
// callback function argument
?>
EXERCISE
<?php
/*
* Test array_map() by passing array
* having reference values for $arr1 argument
*/
echo "Testing array_map() : array with
references for 'arr1' argument<br><br>";
function callback1($a)
{
return ($a);
}
function callback_cat($a, $b)
{
return ($a . $b);
}
// reference variables
$value1 = 10;
$value2 = "hello";
$value3 = 0;
$value4 = &$value2;
// array containing reference variables
$arr1 = array(
0 => 0,
1 => &$value4,
2 => &$value2,
3 => "hello",
4 => &$value3,
$value4 => &$value2
);
echo 'The given $arr1:<br>';
var_dump($arr1);
echo "<br><br>With one array:<br>";
var_dump( array_map('callback1', $arr1) );
echo "<br><br>With two arrays:<br>";
var_dump( array_map('callback_cat', $arr1, $arr1) );
?>
EXERCISE
<?php
/*
* Test array_map() by passing subarrays
* and recursive callback function
*/
echo "Testing array_map() : recursive callback function.<br><br>";
function square_recur_single_array($var) {
if (is_array($var))
return array_map('square_recur_single_array', $var);
return $var * $var;
}
$array1 = array(1, array(2, 3, array(5)), array(4));
var_dump( array_map('square_recur_single_array', $array1));
?>
EXERCISE
<?php
/*
* Test array_map() by passing different
* callback function returning:
* int, string, bool, null values
*/
echo "Testing array_map() : callback with diff return value.<br><br>";
$array1 = array(1, 2, 3);
$array2 = array(3, 4, 5);
echo "With integer return value:<br>";
function callback_int($a, $b)
{
return $a + $b;
}
var_dump( array_map('callback_int', $array1, $array2));
echo "<br><br>With string return value:<br>";
function callback_string($a, $b)
{
return "$a"."$b";
}
var_dump( array_map('callback_string', $array1, $array2));
echo "<br><br>With bool return value:<br>";
function callback_bool($a, $b)
{
return TRUE;
}
var_dump( array_map('callback_bool', $array1, $array2));
echo "<br><br>With null return value:<br>";
function callback_null($array1)
{
return NULL;
}
var_dump( array_map('callback_null', $array1));
echo "<br><br>With no return value:<br>";
function callback_without_ret($arr1)
{
echo "callback_without_ret called\n";
}
var_dump( array_map('callback_without_ret', $array1));
echo "Done";
?>
EXERCISE
<?php
/*
* Test array_map() with a
* pass-by-value callback forced
* to behave as a pass-by-reference function.
*/
$arr1 = array('original.0', 'original.1');
$arr2 = array('original.0', 'original.1');
echo 'The given $arr1:<br>';
var_dump($arr1);
echo '<br><br>The given $arr2:<br>';
var_dump($arr2);
echo '<br><br>';
function callback($a) {
$a = "changed";
}
array_map('callback', $arr1);
var_dump($arr1);
echo '<br><br>';
$ref =& $arr2[0];
array_map("callback", $arr2);
var_dump($arr2);
?>