PHP: Implode a Multidimensional Array

I can’t image this is a common problem, but I recently needed to implode a multidimensional array into all possible combinations for a side project. Since I may need to use this again (or maybe someone else needs something similar), I thought I would post my solution here and attempt to explain it. I also put together a live demo; you might want to check it out before reading the rest of this post.

Final Result

Here is the final result for anyone just looking for the code:

function multidimensional_implode($glue, $pieces) {
    $total = 1;
    foreach ($pieces as $piece)
        $total *= count($piece);

    $variations = array_fill(0, $total, array_fill(0, count($pieces), null));

    foreach ($pieces as $j => $piece) {
        $step = ceil((count($variations) / count($piece)) / (pow(count($piece), $j)));
        for ($i = 0; $i < count($variations); $i++) {
            $k = (floor($i / $step) % count($piece));
            $variations[$i][$j] = $pieces[$j][$k];

    foreach ($variations as $i => $variation)
        $variations[$i] = implode($glue, $variation);

    return $variations;

Now that the copy-pasters have their code, the rest of us can dive in.

Continue reading “PHP: Implode a Multidimensional Array” →

The Five Stages of Insomnia

I actually wrote most of this a few nights ago when I was unable to sleep (shock, I know). The nearly exclusive reason for my occasional insomnia is that my brain is not ready to let me sleep, despite the rest of my body eagerly waiting rest. So there I was, not sleeping, when I realized over the last hour or so I had transitioned through several “phases.” Thinking back on those “phases,” I realized they were noticeably similar to the infamous Five Stages of Grief. Coincidence or not, I had to document my observations.

Stage 1: Denial

The denial stage is not so much about denying you are unable to obtain sleep. Instead, it occurs prior to the lack of obtaining sleep; specifically, when you decide to go to bed for various reasons other than you are literally falling asleep. Perhaps you have to be up earlier than normal the following day, or maybe it is fast approaching 2:15 AM and society tells us that we should be asleep at this time (unless you work a night shift, or you play video games). Regardless of the reason you decide to go to bed, it is not a good enough reason. This is denial, as it relates to insomnia.

Stage 2: Anger

At this point you are lying in your bed (or hanging from the rafters if conventional beds are too cliché for you). You realize you are not sleeping, primarily because you are able to realize you are not sleeping. A few minutes later you grumpily change your position in bed (or face a new wall, for you rafter-dwellers). The anger is slowly building inside you; a few more useless sleeping position adjustments and you may as well be a Sith apprentice.

“Come on,” you angrily think to yourself. “I need to be up in 4 hours. At this rate I will be lucky if I get a solid 3 hours of sleep.” Your anger has caused you to preclude the possibility of falling asleep this very instant, and instead have made the assumption that your lack of rest will continue for a while longer, but hopefully not more than another 60 minutes.

Eventually the anger passes, but you are still left awake.

Continue reading “The Five Stages of Insomnia” →