本文整理汇总了PHP中map函数的典型用法代码示例。如果您正苦于以下问题:PHP map函数的具体用法?PHP map怎么用?PHP map使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了map函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: chain
/**
* Applies a function to items of the array and concatenates the results.
* This is also known as `flatMap` in some libraries.
* ```php
* $words = chain(split(' '));
* $words(['Hello World', 'How are you']) // ['Hello', 'World', 'How', 'are', 'you']
* ```
*
* @signature (a -> [b]) -> [a] -> [b]
* @param callable $fn
* @param array $list
* @return array
*/
function chain()
{
$chain = function ($fn, $list) {
return concatAll(map($fn, $list));
};
return apply(curry($chain), func_get_args());
}
示例2: memoize
/**
* Memoizes callbacks and returns there value instead of calling them
*
* @param callable $callback Callable closure or function
* @param array $arguments Arguments
* @param array|string $key Optional memoize key to override the auto calculated hash
* @return mixed
*/
function memoize($callback, array $arguments = array(), $key = null)
{
Exceptions\InvalidArgumentException::assertCallback($callback, __FUNCTION__, 1);
static $keyGenerator = null, $storage = array();
if (!$keyGenerator) {
$keyGenerator = function ($value) use(&$keyGenerator) {
$type = gettype($value);
if ($type === 'array') {
$key = join(':', map($value, $keyGenerator));
} elseif ($type === 'object') {
$key = get_class($value) . ':' . spl_object_hash($value);
} else {
$key = (string) $value;
}
return $key;
};
}
if ($key === null) {
$key = $keyGenerator(array_merge(array($callback), $arguments));
} else {
$key = $keyGenerator($key);
}
if (!isset($storage[$key]) && !array_key_exists($key, $storage)) {
$storage[$key] = call_user_func_array($callback, $arguments);
}
return $storage[$key];
}
示例3: grid
/**
* Generates a table with a header column and a value column from the given array.
*
* @param mixed $value
* @param string $title [optional]
* @param int $maxDepth [optional] Max. recursion depth.
* @param array $excludeProps [optional] Exclude properties whose name is on the list.
* @param bool $excludeEmpty [optional] Exclude empty properties.
* @param int $depth [optional] For internal use.
* @return string
*/
public static function grid($value, $title = '', $maxDepth = 1, $excludeProps = [], $excludeEmpty = false, $depth = 0)
{
if (is_null($value) || is_scalar($value)) {
return self::toString($value);
}
if ($depth >= $maxDepth) {
return "<i>(...)</i>";
}
if (is_object($value)) {
if (method_exists($value, '__debugInfo')) {
$value = $value->__debugInfo();
} else {
$value = get_object_vars($value);
}
}
if ($title) {
$title = "<p><b>{$title}</b></p>";
}
// Exclude some properties ($excludeProps) from $value.
$value = array_diff_key($value, array_fill_keys($excludeProps, false));
if ($excludeEmpty) {
$value = array_prune_empty($value);
}
return $value ? "{$title}<table class=__console-table><colgroup><col width=160><col width=100%></colgroup>\n" . implode('', map($value, function ($v, $k) use($depth, $maxDepth, $excludeProps, $excludeEmpty) {
$v = self::grid($v, '', $maxDepth, $excludeProps, $excludeEmpty, $depth + 1);
return "<tr><th>{$k}<td>{$v}";
})) . "\n</table>" : '<i>[]</i>';
}
示例4: map
function map($nav, $item = 0, $object)
{
if (!empty($nav[$item])) {
?>
<ul>
<?php
foreach ($nav[$item] as $key => $value) {
?>
<li>
<a href="<?php
echo $object->url(explode('/', $value['location']));
?>
">
<?php
echo $value['name'];
?>
</a>
<?php
map($nav, $key, $object);
?>
</li>
<?php
}
?>
</ul>
<?php
}
}
示例5: solution
function solution($list)
{
$acc = 1;
$func = function ($item, $acc) {
return $acc * $item;
};
$cellItAll = map($list, function ($item) {
//map
return ceil($item);
});
$leaveJustEven = filter($cellItAll, function ($item) {
//filter
return $item % 2 == 0;
});
$multiplyKill = accumulate($leaveJustEven, $func, $acc);
//reduce
###################################################### // one line solution
// return accumulate(filter(map($list, function($item) {
// return ceil($item);
// }), function($item) {
// return $item % 2 == 0;
// }), function($item, $acc) {
// return $acc * $item;
// }, $acc);
return $multiplyKill;
}
示例6: rop_findexec
function rop_findexec()
{
$x = [];
$p = ptr() + 0;
for ($i = 0; $i < 0x2000; $i++) {
array_push($x, [0x41414141, 0x41414141, 0x41414141, 0x41414141, 0x41414141, 0x41414141, 0x41414141, 0x41414141, 0x41414141, 0x41414141, 0x41414141, 0x41414141]);
}
$a = map($p, 0x10000)[0];
nogc($a);
$a = bin2hex($a);
nogc($a);
$r = strpos($a, "3100000000000000100000000f0000000c000000010000000c00000000000000");
if ($r !== FALSE) {
$a = substr($a, $r + 0x80, 0x10);
$a = hexdec(swapEndianness($a));
$a = $a + 0;
for ($i = 0; $i < 0x100000; $i++) {
$k = map(($a & ~0xff) - 0x100 * $i, 0x8);
if ("cffaedfe07000001" == bin2hex($k[0])) {
return ($a & ~0xff) - 0x100 * $i + 0;
}
}
}
return FALSE;
}
示例7: map
function map($fn, $list, $new_list = array())
{
if (!car($list)) {
return $new_list;
}
$new_list[] = $fn(car($list));
return map($fn, cdr($list), $new_list);
}
示例8: test_map
function test_map()
{
$a = function ($x) {
return $x * $x;
};
$range = range(0, 10);
return is_identical(map($a, $range), array_map($a, $range));
}
示例9: testMap
public function testMap()
{
$expectedArr = array(2, 4, 6);
$result = map(array(1, 2, 3), function ($item) {
return $item * 2;
});
$this->assertEquals($result, $expectedArr);
}
示例10: testMap
public function testMap()
{
$range = range(0, 5);
$mapped = map(function ($n) {
return $n * 3;
}, $range);
$this->assertSame([0, 3, 6, 9, 12, 15], toArray($mapped));
}
示例11: shouldCancelInputArrayPromises
/** @test */
public function shouldCancelInputArrayPromises()
{
$mock1 = $this->getMockBuilder('React\\Promise\\CancellablePromiseInterface')->getMock();
$mock1->expects($this->once())->method('cancel');
$mock2 = $this->getMockBuilder('React\\Promise\\CancellablePromiseInterface')->getMock();
$mock2->expects($this->once())->method('cancel');
map([$mock1, $mock2], $this->mapper())->cancel();
}
示例12: map
/**
* Returns a list resulting in invoking a function with each element of a list.
*
* @param callable $proc a function to invoke
* @param Cons $alist a list
*/
function map($proc, $alist)
{
if (isNull($alist)) {
return nil();
} elseif (isPair($alist)) {
return cons($proc(car($alist)), map($proc, cdr($alist)));
} else {
throw new \InvalidArgumentException("{$alist} is not a proper list");
}
}
示例13: __debugInfo
function __debugInfo()
{
$linkToUrl = function (NavigationLinkInterface $link) {
return $link->rawUrl();
};
return ['Current link' => $this->currentLink(), 'All IDs<sup>*</sup>' => PA($this->IDs)->keys()->sort()->join(', ')->S, 'All URLs<sup>*</sup><br>' . '<i>(in scanning order)</i>' => map($this->rootLink->getDescendants(), function (NavigationLinkInterface $link, &$i) {
$i = $link->rawUrl();
return $link->url();
}), 'Trail<sup>*</sup>' => map($this->getCurrentTrail(), $linkToUrl), 'Visible trail<sup>*</sup>' => map($this->getVisibleTrail(), $linkToUrl), 'Navigation map<sup>*</sup>' => iterator_to_array($this->rootLink), 'request' => $this->request()];
}
示例14: test_basic
function test_basic()
{
$function = function ($value) {
return $value * 2;
};
$input = [1, 2, 3];
$expect = [2, 4, 6];
$map = map($function);
$actual = iterator_to_array($map($input));
$this->assertEquals($expect, $actual);
}
示例15: getAll
function getAll()
{
$names = $this->getPropertyNames();
// We must expose the 'macro' property on this method only, so that that macros can be correctly unserialized
$names[] = 'macro';
$x = map($names, function ($v, &$k) {
$k = $v;
return $this->{$k};
});
return $x;
}