Is there simplier than this one in PHP? I want to refactor my code but I can't find and much simplier one. Can you please suggest on how to refactor the code below without using a loop? maybe using only array functions? TIA

&lt;?php
$week_no = array(2,3,4);
$days = array('TU', 'WE', 'TH'); 

foreach ($week_no as $n) {
  foreach ($days as $d) {
   $out[] = $n . $d;
  }
}

var_dump($out); // array('2TU', '2WE', '2TH','3TU', '3WE', '3TH, '4TU', '4WE', '4TH)
?&gt;
How about this for some madness:

function weekday($a, $b){global $days; return "$a $b".join(" $b", $days);};
$out = explode(' ', trim(array_reduce($week_no, 'weekday')));


(Urm, yes well maybe not...)

Sometimes to attain elegance you have to change the way your code is working, i.e. what are your reasons behind generating such an odd array? From my experience something like this would be more useful:

$out = array_fill(reset($week_no),count($week_no),$days);


Which generates the following:

Array
(
    [2] =&gt; Array
        (
            [0] =&gt; TU
            [1] =&gt; WE
            [2] =&gt; TH
        )

    [3] =&gt; Array
        (
            [0] =&gt; TU
            [1] =&gt; WE
            [2] =&gt; TH
        )

    [4] =&gt; Array
        (
            [0] =&gt; TU
            [1] =&gt; WE
            [2] =&gt; TH
        )

)


The above would be much easier to traverse and would be more extendable.

In sticking with your question however the foreach method is by far the best as stated in the comments... but it was fun trying odd work arounds ;) Am surprised that there is no php function to directly prepend or append array items with another set of array items... probably because it's quite easy and fast to do so with a few foreachs.