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
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;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.