最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

post meta - How to merge the array values in foreach?

programmeradmin2浏览0评论

I have tried to get the checkbox values from postmeta field but cant merge the array into single result

$get_info_contact =     get_post_meta( $result, 'wpcf-society-info-contact', true );

$combined = '';
                            foreach($get_info_contact as $contacts){
                                 foreach($contacts as $contact) {
                                     $combined .= 'and';
                                    $combined .= $contact;
                                }
                            }
                            
                            echo $combined;

    Output: 
    
andalcorandpresident

    Expected output:
    
   alcor and president

I have tried to get the checkbox values from postmeta field but cant merge the array into single result

$get_info_contact =     get_post_meta( $result, 'wpcf-society-info-contact', true );

$combined = '';
                            foreach($get_info_contact as $contacts){
                                 foreach($contacts as $contact) {
                                     $combined .= 'and';
                                    $combined .= $contact;
                                }
                            }
                            
                            echo $combined;

    Output: 
    
andalcorandpresident

    Expected output:
    
   alcor and president
Share Improve this question edited Feb 16, 2022 at 9:48 mike asked Feb 16, 2022 at 8:26 mikemike 112 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0
$combined = '';

$k=0;
foreach($get_info_contact as $contacts){
    foreach($contacts as  $contact) {
        //$combined .= 'and';
        //if(in_array($key
        if($k==0){
            $combined .= ucfirst($contact);
        }else{
            $combined .= ' and '.ucfirst($contact);
        }
    }
    
    $k++;
}

output:

   alcor and president
发布评论

评论列表(0)

  1. 暂无评论