PHP Imploding an array into string


Example

implode() combines all the array values but looses all the key info:

$arr = ['a' => "AA", 'b' => "BB", 'c' => "CC"];

echo implode(" ", $arr); // AA BB CC

Imploding keys can be done using array_keys() call:

$arr = ['a' => "AA", 'b' => "BB", 'c' => "CC"];

echo implode(" ", array_keys($arr)); // a b c

Imploding keys with values is more complex but can be done using functional style:

$arr = ['a' => "AA", 'b' => "BB", 'c' => "CC"];

echo implode(" ", array_map(function($key, $val) { 
    return "$key:$val"; // function that glues key to the value
}, array_keys($arr), $arr)); 

// Output: a:AA b:BB c:CC