nathggns / Scaffold

Lightweight PHP API Framework
Other
8 stars 2 forks source link

key_implode should be able to *just* implode pairs, and return an array. #136

Open nathggns opened 11 years ago

nathggns commented 11 years ago

This should work. I'll commit this later.

<?php
/**
 * Implode using keys as well as values
 */
function key_implode($pair_glue, $glue, $arr) {

    $key_arr = [];

    foreach ($arr as $key => $val) {
        $key_arr[] = is_int($key) ? [$val] : [$key, $val];
    }

    $key_arr = array_map(function($val) use($pair_glue) {

        return implode($pair_glue, $val);

    }, $key_arr);

    return $glue ? implode($glue, $key_arr) : $key_arr;
}