Last active
December 18, 2019 14:57
-
-
Save GuyPaddock/9bc96bf7d40c1910bc09575d48b1bc16 to your computer and use it in GitHub Desktop.
How array_reduce() and array_map() Behave with Associative Arrays in PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
On PHP 7.2.24-0ubuntu0.18.04.1 (cli) (built: Oct 28 2019 12:07:07) ( NTS ): | |
Array | |
( | |
[a] => ABC: 1 | |
[b] => ABC: 2 | |
) | |
,1,2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$x = [ | |
'a' => 1, | |
'b' => 2, | |
]; | |
$y = array_map(function ($value) { return 'ABC: ' . $value; }, $x); | |
print_r($y); | |
print "\n"; | |
$z = array_reduce($x, function ($memo, $value) { return $memo . ',' . $value; }); | |
print_r($z); | |
print "\n"; | |
print "\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment