Skip to content

Instantly share code, notes, and snippets.

@GuyPaddock
Last active December 18, 2019 14:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GuyPaddock/9bc96bf7d40c1910bc09575d48b1bc16 to your computer and use it in GitHub Desktop.
Save GuyPaddock/9bc96bf7d40c1910bc09575d48b1bc16 to your computer and use it in GitHub Desktop.
How array_reduce() and array_map() Behave with Associative Arrays in PHP
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
<?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