Shijin TR
Shijin TR

Reputation: 7768

Create time from two array

I have two arrays having hour and minute in PHP ,I want to create the all combination of time from that array.

$hour   =array('1','5');
$minut  =array('30','45');

I have done it with

$result=array();
 foreach($hour as $h){
    foreach($minut as $m){
    $result[]=$h.':'.$m;
   }        
 }

Result

$result=array('1:30','1:45','5:30','5:45');

Is there any other easiest way,Without using nested loop?

Upvotes: 0

Views: 53

Answers (1)

hindmost
hindmost

Reputation: 7195

You may use nested array_map in conjunction with array_walk_recursive:

$result = array();
array_walk_recursive(
    array_map(
        function ($h) use ($minut) {
            return array_map(
                function ($m) use ($h) { return $h.':'.$m; },
                $minut
            );
        },
        $hour
    ),
    function($v, $k) use($key, &$result){
        array_push($result, $v);
    }
);

Upvotes: 1

Related Questions