So a bit of background on what I am doing. I am taking an Excel spreadsheet with a large list of names and emails and sending them a smaller subset of the list based on a series of filters. To accomplish the filtering, I am breaking each Excel row into a JSON array and putting it into the DOM where I then use Javascript/jQuery to populate some hidden fields.
After all the filtering is finished, I have about a dozen comma-delimited lists that I then use PHP explode to turn back into arrays when the form is posted. I need to be able to switch out some variables in the email body with data specific to that person. Without going into too much more detail and risk confusing you, I basically need to replace some values in the body of the email with values from the PHP posted arrays.
I am pulling the posted data in and turning them into arrays with the following code:
//Bring in Hidden fields - turn into arrays
$companies = explode(",", $_POST['companies']);
$kw_signed = explode(",", $_POST['kw_signed']);
$header_rows = explode(",", $_POST['header_rows']);
$first_names = explode(",",$_POST['first_names']);
$emails = explode(",",$_POST['emails']);
$full_names = explode(",",$_POST['full_names']);
$markets = explode(",",$_POST['market']);
$zones = explode(",",$_POST['zones']);
$cities = explode(",",$_POST['cities']);
$states = explode(",",$_POST['states']);
$zipcodes = explode(",",$_POST['zipcodes']);
$p_factors = explode(",",$_POST['perf_factor']);
After I’ve created a bunch of arrays, I am using a for loop to go through the arrays and a foreach to replace the variables in the email body with their respective values, like this:
for($i=0; $i<$count; $i++) {
//Create temp variable for body temp
$body_temp = $email_text;
//Replace header strings with value
foreach($header_rows as $header) {
if($header == '{first_name}') {
$body_temp = str_replace($header, $first_name[$i], $body_temp);
} else if($header == '{email}') {
$body_temp = str_replace($header, $emails[$i], $body_temp);
} else if($header == '{company}') {
$body_temp = str_replace($header, $companies[$i], $body_temp);
} else if($header == '{zone}') {
$body_temp = str_replace($header, $zones[$i], $body_temp);
} else if($header == '{market}') {
$body_temp = str_replace($header, $markets[$i], $body_temp);
} else if($header == '{city}') {
$body_temp = str_replace($header, $cities[$i], $body_temp);
} else if($header == '{state}') {
$body_temp = str_replace($header, $states[$i], $body_temp);
} else if($header == '{zip}') {
$body_temp = str_replace($header, $zipcodes[$i], $body_temp);
} else if($header == '{facility_type}') {
$body_temp = str_replace($header, $facility_type[$i], $body_temp);
}
}
So, now to the question.
How can I do the replace more efficiently?
Do I create an object with array containing the information and loop through the array to replace the object’s variables for each posted name or am I going about this the wrong way entirely? Any insight would be greatly appreciated
You can do replace as below