Add implode_with_keys function

This commit is contained in:
Benjamin Renard 2023-07-10 17:58:56 +02:00
parent 43a467b15a
commit 15c1daabb5
Signed by: bn8
GPG key ID: 3E2E1CE1907115BC

View file

@ -328,4 +328,20 @@ function format_duration($value, $unit=null, $precision=null, $separator=null) {
return implode(is_null($separator)?' ':strval($separator), $result); return implode(is_null($separator)?' ':strval($separator), $result);
} }
/**
* Implode array's keys & values (ex: 'k1=v1, k2=v2, ...')
* @param array<string|int,mixed> $values Array to implode
* @param boolean $quoted Set to false to disable values quotting (optional, default: true)
* @param string $separator Values separator (opional, default: ", ")
* @param string $kv_separator Key/value separator (optional, default: "=")
* @return string Imploded array string
*/
function implode_with_keys($values, $quoted=true, $separator=', ', $kv_separator='=') {
$result = [];
$quoted = $quoted?'"':'';
foreach ($values as $key => $value)
$result[] = "$key$kv_separator$quoted$value$quoted";
return implode($separator, $result);
}
# vim: tabstop=2 shiftwidth=2 softtabstop=2 expandtab # vim: tabstop=2 shiftwidth=2 softtabstop=2 expandtab