Skip to content

Instantly share code, notes, and snippets.

@jimmygle
Last active May 13, 2025 11:27
Show Gist options
  • Save jimmygle/2564610 to your computer and use it in GitHub Desktop.
Save jimmygle/2564610 to your computer and use it in GitHub Desktop.
PHP function to recursively implode multi-dimensional arrays.
<?php
/**
* Recursively implodes an array with optional key inclusion
*
* Example of $include_keys output: key, value, key, value, key, value
*
* @access public
* @param array $array multi-dimensional array to recursively implode
* @param string $glue value that glues elements together
* @param bool $include_keys include keys before their values
* @param bool $trim_all trim ALL whitespace from string
* @return string imploded array
*/
function recursive_implode(array $array, $glue = ',', $include_keys = false, $trim_all = true)
{
$glued_string = '';
// Recursively iterates array and adds key/value to glued string
array_walk_recursive($array, function($value, $key) use ($glue, $include_keys, &$glued_string)
{
$include_keys and $glued_string .= $key.$glue;
$glued_string .= $value.$glue;
});
// Removes last $glue from string
strlen($glue) > 0 and $glued_string = substr($glued_string, 0, -strlen($glue));
// Trim ALL whitespace
$trim_all and $glued_string = preg_replace("/(\s)/ixsm", '', $glued_string);
return (string) $glued_string;
}
@KarelWintersky
Copy link

$arr = [
    "INTO xxx",
    "(",
    [ 'a', 'b', 'c' ],
    ")",
    "VALUES",
    "(",
    [ ':a', ':b', ':c' ],
    ")"
];

var_dump(recursive_implode($arr, ", ", false, false));

Return:

"INTO xxx, (, a, b, c, ), VALUES, (, :a, :b, :c, )"

does not work as expected 😢

@mister-pc
Copy link

Here's my version :-)
It doesn't use regexp, and deals with associative arrays but also objects, and makes key display as optional.
The output format is the most intuitive and can easily be used for debugging, it looks a bit like the var_dump one.
For example :
0 => [ 0 => [ 'id' => "1d8e9eb4", 'key' => "value", 'settings' => { 'width' => "full" }, 'empty_object' => { }, 'empty_array' => [ ] ], 1 => "string_value" ]

function recursive_implode( string $separator, mixed $mixed, bool $display_keys = false ) {
    $output = "";
    $value_index = 0;
    if( is_array( $mixed ) || is_object( $mixed ) ) {
        foreach( $mixed as $key => $value ) {
            $output .= ( $value_index ? $separator : '' ) . ( $display_keys ? ( is_int( $key ) ? $key : "'" . $key . "'" ) . ' => ' : '' );
            if( is_array( $value ) ) {
                $array_ouput = recursive_implode( $separator, $value, $display_keys );
                if( $array_ouput ) {
                    $output .= '[ ' . $array_ouput . ' ]';
                } else {
                    $output .= '[]';
                }
            } else
            if( is_object( $value ) ) {
                $object_output = recursive_implode( $separator, $value, $display_keys );
                if( $object_output ) {
                    $output .= '{ ' . $object_output . ' }';
                } else {
                    $output .= '{}';
                }
            } else {
                $output .= '"' . $value . '"';
            }
            $value_index++;
        }
    } else {
        $output = $mixed;
    }
    return $output;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment