Skip to content

Instantly share code, notes, and snippets.

@jimmygle
Last active June 20, 2024 22:01
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;
}
@sergeydevhub
Copy link

Like a boss.

@kachmi
Copy link

kachmi commented Nov 18, 2015

Here is a simple answer:

function implode_recur ($separator, $arrayvar){
$output = " ";
foreach ($arrayvar as $av)
if (is_array ($av)) $out .= implode_r ($separator, $av); // Recursive Use of the Array
else $out .= $separator.$av;

return $output;
}

$result = implode_recur(">>",$variable);

Okay have a good coding!

@bartrail
Copy link

I fixed and modified @kachmi's simpler answer a bit (added php7.4 types, added braces, renamed variables so it's working and added check if the separator is actually needed)

function implode_recursive(string $separator, array $array): string
{
    $string = '';
    foreach ($array as $i => $a) {
        if (is_array($a)) {
            $string .= implode_recursive($separator, $a);
        } else {
            $string .= $a;
            if ($i < count($array) - 1) {
                $string .= $separator;
            }
        }
    }

    return $string;
}

@lerminou
Copy link

lerminou commented Aug 6, 2021

@bartrail your answer doesn't work for associatives arrays

@theofanisv
Copy link

Thank you for your work

@Manzadey
Copy link

Manzadey commented Jun 6, 2022

Thank you! It's great working.

@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 😢

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