Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8595823
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:31:53+00:00 2026-06-12T00:31:53+00:00

So a bit of background on what I am doing. I am taking an

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T00:31:54+00:00Added an answer on June 12, 2026 at 12:31 am

    You can do replace as below

    for($i=0; $i<$count; $i++) {
        //Create temp variable for body temp    
        $body_temp = $email_text;
        $body_temp = str_replace(array('{firsname}','{email}'), array($first_name[$i],$emails[$i]), $body_temp);  
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For a bit of background to know what i am doing: using hit highlighter
I'm a bit stumped. I've worked with background workers before, and have them in
For a bit of background, I'm writing a meter reading application in C for
Good morning, A bit of background might help. I'm creating a very simple app
Background I'm having a bit of trouble finding out just how to programmatically post
I have to run a bit of code in the background every one second,
This feature worked a bit, then stopped working. Background: Given a user is logged
A bit of background info; this is an application that allows users to created
Background: I've been doing RoR for about a year now, and am fairly comfortable
Background I'm currently doing a benchmark of solutions to convert java code to c#

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.