How to optimize the array_map function of PHP (how to input parameters)

Source: Internet
Author: User
{Code...} test {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 < $t2){        $arrHis[] = date($format,$t1);        $t1 += $range;    }    return $arrHis;//    return array_map(function ($time) {//        return date($format, $time);//    }, range(strtotime($hisStart), strtotime($hisEnd), $range));}
Test
$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)*/
Problem
How to pass $ format to array_map

Reply content:
/** * @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 < $t2){        $arrHis[] = date($format,$t1);        $t1 += $range;    }    return $arrHis;//    return array_map(function ($time) {//        return date($format, $time);//    }, range(strtotime($hisStart), strtotime($hisEnd), $range));}
Test
$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)*/
Problem
How to pass $ format to array_map

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

Keywords:Closure

The optimal solution is to use it like @ picasso250.

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

If you cannot use the use keyword, you can use global:

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

Of course, it is also possible to write this with Class, that is, it looks particularly troublesome...

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();

Okay, I just want to provide different solutions...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.