dongta5621 2017-11-13 07:15
浏览 44
已采纳

string连接全局变量

I have fetched the value from database using query and trace the variable using foreach. I want to get the mail id append with the variable $cc_test.

Initially the variable is empty. for first iteration test@sample.com 2nd iteration test1@sample.com and for 3rd test2@sample.com.

Finally I want to get test@sample.com,test1@sample.com,test2@sample.com, but now I get 'test2@sample.com,test2@sample.com'.

$cc_check is an indexed array print_r($cc_test) in for loop prints test@sample.com for 1st and test1@sample.com for 2nd and so on.

$cc_check=asset_status_tracker::where('request_id','=',$data['id'])->where('status_before','!=',null)->get();
$cc_test=''; //dump requestor mail id
foreach($cc_check as $cc_inner){
    $cc_mail=$cc_inner->changed_by;
    print_r($cc_mail);
    $cc_test=users::where('id','=',$cc_mail)->first()->mail;
    $cc_test=$cc_test.','.$cc_test;
    //print_r($cc_test); 
}
print_r($cc_test); //test@sample.com,test1@sample.com,test2@sample.com
  • 写回答

1条回答 默认 最新

  • dongmeiba6151 2017-11-13 07:22
    关注

    If I understand you, you don't need a counter, you just need to concatenate all of the values using commas as glue.

    You can do this without excess commas or iterating conditions by running implode() after the loop is finished.

    Save the emails in an array, and implode the array to generate a string of comma-separated values.

    Code:

    $emails=[];
    foreach($cc_check as $cc_inner){
        $emails[]=users::where('id','=',$cc_inner->changed_by)->first()->mail;
    }
    $email_string=implode(',',$emails);
    echo $email_string;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)