I have an array in a function (get_travel_reports_places()
) called $places
in which several locations are saved in (e.g. "Bangkok, Thailand", "Vientiane, Laos", ...).
I now want to put the content of this array into a variable which I can return. The goal is to call the function like this
<?php echo get_travel_reports_places(); ?>
and get a list of the places, seperated with a |
:
Bangkok, Thailand|Vientiane Laos
I tried to do it with a foreach
loop, but this only works when I echo
the places directly. Storing them in a single variable doesn't work.
Anyone willing to help me out?
You can do that using join(), which is an alias of implode
join("|", $places);
Take a look at the implode function.
return implode('|', $places);
$places = get_travel_reports_places();
echo implode('|', $places);
should do it.
To correct your question, you want to return a string, not an array.
The code would be as follows:
function get_travel_report_places() {
// your regular code here
return implode(' | ', $places);
}
This function will join all the places in the $places
array and return a string with the places separated by |
.