I’ve an array:
JavaScript
x
[
"Cuzco" => "Peru"
"Lima" => "Peru"
]
I need the output to be: Cuzco (Peru) + Lima (Peru)
Can I use http_build_query
for this? I tried
JavaScript
http_build_query($array, " + ")
Advertisement
Answer
Put all the key (value)
strings in an array, then call implode()
to combine them.
JavaScript
$a = [];
foreach ($array as $key => $value) {
$a[] = "$key ($value)";
}
$result = implode(' + ', $a);