加入 2 个回声并用逗号分隔 - 内爆,数组

Join 2 echoes and separate with coma - implode, array

我有 2 个变量。第一个来自分类并输出分类术语,第二个输出自定义字段:

$address_city = get_custom_field( 'address_city' );
        echo '<span>' . $address_city . '</span>';

$terms = get_the_terms($post->id, 'listing_country');
    foreach ( $terms as $term ) {
        echo '<span>' . $term->name . '</span>';
}

结果如下所示:CityCountry

我想弄清楚如何用一个彗差将这两个回声分开,,所以结果如下:City, Country

我一直在尝试使用 implode()array(),但我就是想不出如何在没有错误的情况下完成这项工作。

$address_city = get_custom_field( 'address_city' );
        $arr[0] = '<span>' . $address_city . '</span>';

$terms = get_the_terms($post->id, 'listing_country');
    foreach ( $terms as $term ) {
        $arr[1] = '<span>' . $term->name . '</span>';
}    
    echo implode(", ", $arr);

试试这个代码,它肯定能帮到你..