Heim >Backend-Entwicklung >PHP-Tutorial >PHP 的这个函数array_map如何优化(如何传入参数)

PHP 的这个函数array_map如何优化(如何传入参数)

WBOY
WBOYOriginal
2016-06-06 20:41:591644Durchsuche

<code>/**
 * @param string $hisStart
 * @param string $hisEnd
 * @param int    $range
 * @param string $format
 *
 * @return array
 */
function createHisRange($hisStart = '00:00:00', $hisEnd = '23:59:59', $range = 3600, $format = 'H:00:00')
{
    $t1 = strtotime($hisStart);
    $t2 = strtotime($hisEnd);
    $arrHis = array();
    while($t1 </code>

测试

<code>$s = createHisRange();
print_r($s);
/*
Array
(
    [0] => 00:00:00
    [1] => 01:00:00
    [2] => 02:00:00
    [3] => 03:00:00
    [4] => 04:00:00
    [5] => 05:00:00
    [6] => 06:00:00
    [7] => 07:00:00
    [8] => 08:00:00
    [9] => 09:00:00
    [10] => 10:00:00
    [11] => 11:00:00
    [12] => 12:00:00
    [13] => 13:00:00
    [14] => 14:00:00
    [15] => 15:00:00
    [16] => 16:00:00
    [17] => 17:00:00
    [18] => 18:00:00
    [19] => 19:00:00
    [20] => 20:00:00
    [21] => 21:00:00
    [22] => 22:00:00
    [23] => 23:00:00
)

*/

</code>

问题

<code>如何将 $format 传入到 array_map 当中
</code>

回复内容:

<code>/**
 * @param string $hisStart
 * @param string $hisEnd
 * @param int    $range
 * @param string $format
 *
 * @return array
 */
function createHisRange($hisStart = '00:00:00', $hisEnd = '23:59:59', $range = 3600, $format = 'H:00:00')
{
    $t1 = strtotime($hisStart);
    $t2 = strtotime($hisEnd);
    $arrHis = array();
    while($t1 </code>

测试

<code>$s = createHisRange();
print_r($s);
/*
Array
(
    [0] => 00:00:00
    [1] => 01:00:00
    [2] => 02:00:00
    [3] => 03:00:00
    [4] => 04:00:00
    [5] => 05:00:00
    [6] => 06:00:00
    [7] => 07:00:00
    [8] => 08:00:00
    [9] => 09:00:00
    [10] => 10:00:00
    [11] => 11:00:00
    [12] => 12:00:00
    [13] => 13:00:00
    [14] => 14:00:00
    [15] => 15:00:00
    [16] => 16:00:00
    [17] => 17:00:00
    [18] => 18:00:00
    [19] => 19:00:00
    [20] => 20:00:00
    [21] => 21:00:00
    [22] => 22:00:00
    [23] => 23:00:00
)

*/

</code>

问题

<code>如何将 $format 传入到 array_map 当中
</code>

<code>    return array_map(function ($time) use ($format) {
        return date($format, $time);
    }, range(strtotime($hisStart), strtotime($hisEnd), $range));
</code>

关键字:闭包

最优解当然是像 @picasso250 一样用use了

<code> return array_map(function ($t) use ($f) {
    return date($f, $t);
}, range(strtotime($s), strtotime($e), $r));
</code>

如果用不了use关键字的话低版本的话还可以用global:

<code>$format = "H:i:s";
function fm($t){
    global $format;
    return date($format, $time);
}
array_map('fm', range(strtotime($hisStart), strtotime($hisEnd), $range));
</code>

当然用Class写这个也是可以的,就是看起来特别麻烦...

<code>Class hisRange {
    public $s, $e, $r, $f;

    function __constructor($s= '00:00:00', $e= '23:59:59', $r= 3600, $f= 'H:00:00') {
        $this->s = strtotime($s);
        $this->e = strtotime($e);
        $this->r = $r;
        $this->f = $f;
    }

    function run() {
         return array_map(array($this, 'fm'), range($this->s, $this->e, $this->r));
    }

    function fm($t) {
        return date($this->f, $t);
    }
}

$range = new hisRange();
$s = $range->run();
</code>

好吧,我大概只是来提供不同的解法的...

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn