PHP Executing Upon an Array Imploding an array into string

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

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


Got any PHP Question?