当前位置: 首页>>代码示例>>PHP>>正文


PHP map函数代码示例

本文整理汇总了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());
}
开发者ID:tarsana,项目名称:functional,代码行数:20,代码来源:list.php

示例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];
}
开发者ID:reeze,项目名称:functional-php,代码行数:35,代码来源:Memoize.php

示例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>';
 }
开发者ID:impactwave,项目名称:php-web-console,代码行数:39,代码来源:Debug.php

示例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 
    }
}
开发者ID:cheevauva,项目名称:trash,代码行数:28,代码来源:FrontendIndex.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;
}
开发者ID:4johndoe,项目名称:hexlet,代码行数:26,代码来源:reduce.php

示例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;
}
开发者ID:kjungi704,项目名称:phpmess,代码行数:25,代码来源:pm_rop_findexec_osx.php

示例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);
}
开发者ID:emkay,项目名称:php-phant,代码行数:8,代码来源:phpphant.php

示例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));
}
开发者ID:Burgestrand,项目名称:Funcy,代码行数:8,代码来源:funcy.php

示例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);
 }
开发者ID:kiasaki,项目名称:vexillum,代码行数:8,代码来源:FunctionsTest.php

示例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));
 }
开发者ID:rexfordkelly-on-JS,项目名称:iter,代码行数:8,代码来源:iterTest.php

示例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();
 }
开发者ID:reactphp,项目名称:promise,代码行数:9,代码来源:FunctionMapTest.php

示例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");
    }
}
开发者ID:mudge,项目名称:php-microkanren,代码行数:16,代码来源:Lisp.php

示例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()];
 }
开发者ID:electro-framework,项目名称:framework,代码行数:10,代码来源:Navigation.php

示例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);
 }
开发者ID:morrisonlevi,项目名称:algorithm,代码行数:11,代码来源:MapTest.php

示例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;
 }
开发者ID:electro-modules,项目名称:matisse,代码行数:11,代码来源:MacroCallProperties.php


注:本文中的map函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。